尝试将vars发送到url中的服务器。 (脚本可以通过手动网址找到)。 var被拾取并显示在日志中,但是在我在此处显示的代码部分中,URL没有调用脚本:String urlString = SERVER + this.getString(R.string.login_details_url); (顺便说一句,在其他工作活动中调用的常量中,php文件名的拼写正确)。
我认为这可能与方法设置方式有关。服务器似乎有有效的响应,这最终说明了激活的成功吐司。这里缺少什么?
谢谢
private void acctinfo(String username, String email, String password, String usertype, String over35, String userid) {
Log.d(DEBUG_TAG, "AcctInfo() entered:" + username + email + password + usertype + over35 + userid); /// These vars show up in logs
try
{
sendAcctInfoToServer(this, username, email, password, usertype, over35, userid);
}
catch(Exception e)
{
e.printStackTrace();
Log.d(DEBUG_TAG, "sendAcctInfoToServer error " , e);
Toast.makeText(mContext, "Failed to Subit New Info", Toast.LENGTH_SHORT).show();
}
}
private void sendAcctInfoToServer(Context context, String username, String email, String password, String usertype, String over35, String userid) throws Exception
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
String urlString = SERVER + this.getString(R.string.login_details_url);
urlString = urlString + "?" + USEREMAIL_VAR + "=" + email;
urlString = urlString + "&" + PASSWORD_VAR + "=" + password;
urlString = urlString + "&" + USERNAME_VAR + "=" + username;
urlString = urlString + "&" + USERID_VAR + "=" + userid;
urlString = urlString + "&" + USERTYPE_VAR + "=" + usertype;
urlString = urlString + "&" + OVER35_VAR + "=" + over35;
urlString = urlString+ "&change_acct=1";
HttpPost httppost = new HttpPost(urlString); //////This url does not get sent
HttpResponse response =httpclient.execute(httppost);
// Check if server response is valid
String result = null;
StatusLine status = response.getStatusLine();
Log.d(DEBUG_TAG, " AcctInfo response.getStatusLine() " + status.getStatusCode());
if (status.getStatusCode() != HttpURLConnection.HTTP_OK) {
result = "Invalid response from server, status code=" + status.toString();
// Failed - so throw
throw new Exception("Please try again" + result); //TODO external string, remove details of error
}
else {
//toast
Toast.makeText(mContext, getString(R.string.toast_acct_info_saved), Toast.LENGTH_SHORT).show(); /// this success toast appears but no vars, of course, are passed to script
finish();
}
}
最佳答案
尝试使用android Uri对象构建您的URL。
String baseUrl = getResources().getString(R.string.login_details_url);
Uri.Builder builder = Uri.parse(baseUrl).buildUpon()
.appendQueryParameter(USEREMAIL_VAR, email)
.append...;
HttpPost httppost = new HttpPost(builder.build());
HttpResponse response = httpclient.execute(httppost);
也许您的问题是由于错误的字符编码引起的。
要记录响应,您可以从response.getEntity()。getContent()创建一个InputStreamReader。
我不知道哪个min-sdk版本使用您的应用程序,但是有一个AndroidHttpClient可以设置很多默认值。我的请求使用DefaultHttpClient失败,但是使用AndroidHttpClient成功。
我没有直接使用AndroidHttpClient(API8),但是得到了代码(删除了curl的部件)。
raw AndroidHttpClient sources
请注意,您不能在主线程中使用AndroidHttpClient,而必须在新的AsyncTask或runnable + thread中执行查询。
关于java - Android代码中扭曲的Java方法需要良好的关注,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5302527/