当前位置: 动力学知识库 > 问答 > 编程问答 >

android - onPostExecute not executed

问题描述:

I have an AsyncTask class in my app that connects to an external database. After the task is (successfully) completed I want to start a new activity.

MainActivity

public class MainActivity extends Activity implements OnClickListener, PostKey.AsyncResponse {

PostKey asyncTask = new PostKey();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

asyncTask.delegate = this;

}

public void processFinish(String output){

//this you will received result fired from async class of onPostExecute(result) method.

String string;

string = "test";

Log.d(string, " processFinish");

}

public void onClick(View v) {

keyValue = key.getText().toString(); // Is declared at the top didn't include it in this snippet

String pattern = "^[a-zA-Z0-9]*$";

if(keyValue.length() < 5){

new PostKey().execute(keyValue);

}

}

}

PostKey

public class PostKey extends AsyncTask<String, Integer, Double> {

public AsyncResponse delegate = null;

@Override

protected Double doInBackground(String... params) {

postData(params[0]);

return null;

}

//@Override can't add override, it doesn't override method from its superclass

protected void onPostExecute(String result){

delegate.processFinish(result);

}

public void postData(String valueIWantToSend) {

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http:/www.domain.com/post.php");

try {

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("x", "test"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

String responseStr = EntityUtils.toString(response.getEntity());

Log.d(responseStr, ""); // This gets logged

if(responseStr == "true"){

delegate.processFinish(responseStr); // This doesn't execute the processFinish in mainActivity either

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

} catch (IOException e) {

// TODO Auto-generated catch block

}

}

public interface AsyncResponse {

void processFinish(String output);

}

}

网友答案:
//@Override can't add override, it doesn't override method from its superclass
protected void onPostExecute(String result)

Take the error as a hint: Your method signature is wrong. It should be

@Override
protected void onPostExecute(Double result)

or since you're not really returning any Doubles, just change the result type declaration in extends AsyncTask<String, Integer, Double>.

分享给朋友:
您可能感兴趣的文章:
随机阅读: