-1

在阅读了向 ui 发送回调的 android asynctask我的目标是:

当您等待来自服务器的身份验证时,将显示对话框,只要该过程完成,将调用回调并关闭对话框。

我的结构是( asyntask 和登录活动是分开的类)

  1. 登录活动
  2. A 类扩展 Aysnctask
  3. 回调接口。

我的部分代码:

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() {

    }
}

服务.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抛出在 cat 日志中。关于这个家伙的任何想法。

PS:

登录我这里的

来自链接的 logcat

4

1 回答 1

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;
}
于 2013-05-08T14:38:28.783 回答