阅读android asynctask sending callbacks to ui后我的目标是:
当您等待身份验证时,将显示对话框 服务器,只要进程完成,回调就会 调用和对话框将被取消。
我的结构是(asyntask和登录活动是分开的类)
- 登录活动
- A类扩展了Aysnctask
- 回叫接口。
醇>
我的部分代码:
public class SpalshScreenActivity extends Activity implements LogInCallBack{
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
logInButton = (Button) findViewById(R.id.btnLogin);
logInButton.setBackgroundDrawable(getResources() .getDrawable(R.drawable.button));
logInButton .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = new ProgressDialog(SpalshScreenActivity.this);
dialog.setMessage("waiting......");
dialog.show();
Service client = new Service(new SpalshScreenActivity(), email.getText().toString(), password.getText().toString());
String logInURL = "my url goes here";
client.execute(logInURL);
}
});
}
}
@Override
public void successfullyLogIn() {
Log.d("SPLASH","successfullyLogIn");
dialog.dismiss(); // **<--------crash is at here**
}
@Override
public void failedToLogIn() {
}
}
Service.java:
public class Service extends AsyncTask<String, Integer, String> {
private LogInCallBack callBack;
String userName;
String password;
public Service(LogInCallBack callBack, String userName, String password) {
this.callBack = callBack;
this.userName = userName;
this.password = password;
}
// Handling ssl connection
protected String doInBackground(String... arg) {
// validation and accept all type of self signed certificates
SimpleSSLSocketFactory simpleSSLFactory;
String logInURL = arg[0];
try {
simpleSSLFactory = new SimpleSSLSocketFactory(null);
simpleSSLFactory .setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
// Enable HTTP parameters
params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
// Register the HTTP and HTTPS Protocols. For HTTPS, register our
// custom SSL Factory object.
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80));
registry.register(new Scheme("https", simpleSSLFactory, 443));
// Create a new connection manager using the newly created registry
// and then create a new HTTP client
// using this connection manager
ccm = new ThreadSafeClientConnManager(params, registry);
} catch (KeyManagementException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnrecoverableKeyException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (KeyStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient(ccm, params);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("user_session[email]", "lenanguyen@hotmail.ca"));
nameValuePair.add(new BasicNameValuePair("user_session[password]","2congato"));
// Create http GET method
HttpPost logIn = new HttpPost(logInURL);
try {
logIn.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Fire a request
try {
HttpResponse response = client.execute(logIn);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
this.result = convertStreamToString(is);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("ClientProtocolException is ", e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("IOException is ", e.toString());
}
return result;
}
// called after doInBackground finishes
protected void onPostExecute(String result) {
Log.e("result, yay!", this.result);
this.callBack.successfullyLogIn();
}
}
我的回叫界面是
public interface LogInCallBack {
void successfullyLogIn ();
void failedToLogIn();
}
然而,在对话框显示几秒钟后,我的应用程序崩溃了,并且猫日志中抛出了FATAL Exception in MAIN
。关于这个人的任何想法。
PS:
log cat i here
答案 0 :(得分:2)
更改
Service client = new Service(new SpalshScreenActivity(), email.getText().toString(), password.getText().toString());
到
Service client = new Service(SpalshScreenActivity.this, email.getText().toString(), password.getText().toString());
和
public FlipGiveClient(LogInCallBack callBack, String userName, String password) {
this.callBack = callBack;
this.userName = userName;
this.password = password;
}
进入
public FlipGiveClient(Activity activity, String userName, String password) {
this.callBack = (LogInCallBack) activity;
this.userName = userName;
this.password = password;
}