出于某种原因,我的 doinbackground 方法执行但 onPost 执行没有我放置覆盖注释并匹配参数,我也在做 task.execute 而不是仅仅调用 doinbackground 但它仍然没有执行。这就是我的代码的样子:
安卓活动:
package com.Keb3mr.workoutscheduler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import com.Keb3mr.workoutscheduler.MyApp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements HttpCallback, OnClickListener{
private EditText user_;
private EditText pass_;
private Button login_;
private Button signUp_;
private MyApp appState_;
private String url_ = "http://192.168.1.110:8888/vshare/account/create";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Workout Scheduler");
MyApp appState = ((MyApp)getApplicationContext());
appState_ = appState;
user_ = (EditText) findViewById(R.id.userText);
pass_ = (EditText) findViewById(R.id.passwordText);
login_ = (Button) findViewById(R.id.loginButton);
signUp_ = (Button) findViewById(R.id.signupButton);
appState_.setUser("default");
appState_.setPass("default");
login_.setOnClickListener(this);
signUp_.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.loginButton){
Log.i("amre", "button pushed");
appState_.setUser(user_.getText().toString());
appState_.setPass(pass_.getText().toString());
try {
login(url_);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent i = new Intent(this, ViewCreate.class);
startActivity(i);
}
else if(v.getId() == R.id.signupButton){
appState_.setUser(user_.getText().toString());
appState_.setPass(pass_.getText().toString());
}
}
public void login(String server) throws Exception{
Log.i("button", "login");
MultipartEntity entity = new MultipartEntity();
entity.addPart("user", new StringBody(appState_.getUser()));
entity.addPart("pass", new StringBody(appState_.getPass()));
LoginTask t = new LoginTask(entity, url_, this);
t.execute();
Log.i("Task", "Executed");
}
public void onResponse(HttpResponse resp) {
String line = null;
InputStream is = null;
try {
is = resp.getEntity().getContent();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e1) {
e1.printStackTrace();
}
String result = sb.toString();
Log.i("reuslt:", result);
}
public void onError(Exception e) {
// TODO Auto-generated method stub
}
}
我在调用 t.execute 的 onclick 方法中调用 login。这就是我的异步任务的样子:
package com.Keb3mr.workoutscheduler;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
public class LoginTask extends AsyncTask<Void, Void, HttpResponse> {
private HttpEntity entity_;
private String url_;
private HttpCallback callback_;
public LoginTask(HttpEntity entity, String url, HttpCallback callback){
super();
entity_ = entity;
url_ = url;
callback_ = callback;
}
@Override
protected void onPostExecute(HttpResponse result) {
// TODO Auto-generated method stub
Log.i("onPost", "executing");
super.onPostExecute(result);
callback_.onResponse(result);
}
@Override
protected HttpResponse doInBackground(Void... params) {
// TODO Auto-generated method stub
Log.i("doinbackground", "executing");
HttpResponse resp = null;
try{
HttpPost post = new HttpPost(url_);
Log.i("doinbackground", "httppst");
post.setEntity(entity_);
Log.i("doinbackground", "setentity");
HttpClient client = new DefaultHttpClient();
Log.i("doinbackground", "client");
resp = client.execute(post);
Log.i("doinbackground", "resp");
}
catch(Exception e){
Log.i("doinbackground", "error");
e.printStackTrace();
}
return resp;
}
}