由于某些原因,这不起作用。我确实得到了doInBackground的响应,并将其保存在结果中。我能够在doInBackground之后看到结果,并且收到了正确的结果,只是onPostExecute没有运行。
private class AttemptLogin extends AsyncTask<String, Void, Void>
{
String result;
TextView error =(TextView)findViewById(R.id.errorlogin);
@Override
protected void onPreExecute() {}
@Override
protected Void doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
nameValuePairs.add(new BasicNameValuePair("username", params[0]));
nameValuePairs.add(new BasicNameValuePair("password", params[1]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
System.out.print(nameValuePairs);
result = httpclient.execute(httppost, new BasicResponseHandler());
System.out.print(result);
//System.out.println("check");
nameValuePairs.clear();
} catch (ClientProtocolException e) {
System.out.println("check1");
} catch (IOException e) {
System.out.println(e.getMessage());
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {}
@Override
protected void onPostExecute(Void voids) {
super.onPostExecute(voids);
if (result.equals("Success"))
{validate();
System.out.print(result);}
else if (result.equals("Failure")){
System.out.print(result);
error.setText("Don't match");
}
else{
System.out.print("no response");
}
}
}
答案 0 :(得分:1)
我认为你必须将结果作为参数传递,就像它应该是:
检查此样本:
class AttemptLogin extends AsyncTask<String, Void, String>
{
TextView error =(TextView)findViewById(R.id.errorlogin);
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... params) {
if (params[0].equals(""))
return null;
String result = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
nameValuePairs.add(new BasicNameValuePair("username", params[0]));
nameValuePairs.add(new BasicNameValuePair("password", params[1]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
System.out.print(nameValuePairs);
result = httpclient.execute(httppost, new BasicResponseHandler());
System.out.print(result);
//System.out.println("check");
nameValuePairs.clear();
return result.trim();
} catch (ClientProtocolException e) {
System.out.println("check1");
return null;
} catch (IOException e) {
System.out.println(e.getMessage());
return null;
}
}
@Override
protected void onProgressUpdate(Void... values) {}
@Override
protected void onPostExecute(String result) {
if (result.equals("Success"))
{
validate();
System.out.print(result);}
}
else if (result.equals("Failure")){
System.out.print(result);
error.setText("Don't match");
}
else{
System.out.print("no response");
}
}
}