【发布时间】:2015-06-25 15:04:16
【问题描述】:
我正在向 Web 服务器发送HTTP post 请求以进行登录。它返回字符串值true 或false。
AsyncTask代码:
class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{
HttpResponse httpResponse;
@Override
protected String doInBackground(String... params) {
String paramUsername = params[0];
String paramPassword = params[1];
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("myurl");
try {
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("user", paramUsername);
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("password", paramPassword);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePAir);
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
httpPost.setEntity(urlEncodedFormEntity);
httpResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException cpe) {
System.out.println("First Exception caz of HttpResponese :" + cpe);
} catch (IOException ioe) {
System.out.println("Second Exception caz of HttpResponse :" + ioe);
}
return httpResponse.toString();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String s="true";
if(result.equalsIgnoreCase(s)){
Toast.makeText(getApplicationContext(), "Congrats! Login Successful...", Toast.LENGTH_LONG).show();
Intent intent = new Intent(SignIn.this, Dashboard.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "Invalid Username or Password...", Toast.LENGTH_LONG).show();
}
}
}
OnCreate代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
editTextUserName = (EditText) findViewById(R.id.editTextUserNameToLogin);
editTextPassword = (EditText) findViewById(R.id.editTextPasswordToLogin);
Button btnSignIn = (Button) findViewById(R.id.buttonSignIn);
// btnSignIn.setOnClickListener(this);
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//if (v.getId() == R.id.buttonSignIn) {
String givenUsername = editTextUserName.getEditableText().toString();
String givenPassword = editTextPassword.getEditableText().toString();
// System.out.println("Given username :" + givenUsername + " Given password :" + givenPassword);
new SendPostReqAsyncTask().execute(givenUsername, givenPassword); } }); }
将doInBackground 的返回值更改为httpResponse.toString() 也会导致应用崩溃。
我是 Android 新手,即使经过大量搜索似乎也无法解决问题。任何帮助表示赞赏。
编辑:httpResponse 可以通过以下操作转换为字符串:
String response = EntityUtils.toString(httpResponse.getEntity());
【问题讨论】:
-
您从 doInBackground 返回的内容是您在 onPostExecute 中作为参数接收的内容。在您的情况下,您将返回 null。提示:对 REST API 请求使用改造,这会容易得多。
-
试试这个
editTextUserName.getText() -
您正在从 doInBackground 返回 null 到 onPostExecute,这就是它在 result.equalsIgnoreCase(s) 处提供 NPE 的原因
-
onPostExecute你的result值是null。你能说出崩溃的行号吗? -
但是你在 doInBackground 方法中返回 null 并且你的代码不完整
标签: android android-asynctask http-post