Android:即使使用AsyncTask也会在主线程异常上建立网络

时间:2015-07-15 12:35:54

标签: java android multithreading android-asynctask

我正在开发一个Android项目,而我正在与服务器进行通信以进行身份​​验证。即使我使用的是AsyncTask,我也会收到NetworkOnMain线程异常。这是为什么。这是我的代码:

public class Login extends Activity {

    public static RestTemplate rest = new RestTemplate();
    Button loginButton = (Button) findViewById(R.id.LoginButton);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!(v == null)) {
                    EditText userEmail = (EditText) findViewById(R.id.usernameText);
                    EditText userPassword = (EditText) findViewById(R.id.PasswordField);
                    if (!(userEmail.getText().toString().isEmpty())) {
                        if (!(userPassword.getText().toString().isEmpty())) {
                            loginUserViaRest(userEmail.getText().toString(), userPassword.getText().toString());
                            if (!(StaticRestTemplate.jsessionid == null)) {
                                if (!(StaticRestTemplate.jsessionid.isEmpty())) {

                                    executeRestLogin executeRestLogin = new executeRestLogin();
                                    executeRestLogin.doInBackground();

 private class executeRestLogin extends AsyncTask<Void,Void,Void>{

        @Override
        protected Void doInBackground(Void... params) {
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.add("Cookie", "JSESSIONID=" + StaticRestTemplate.jsessionid);
            HttpEntity<String> requestEntity = new HttpEntity<>(requestHeaders);

// Below line gives me an error
            HttpEntity<String> rssResponse = rest.exchange(
                    "http://192.168.178.60:8080/dashboard",
                    HttpMethod.GET,
                    requestEntity,
                    String.class);
            StaticRestTemplate.jsessionid = rssResponse.getBody();
            return null;
        }
    }

我做错了什么。另外,如何从异步任务中获取自定义对象作为响应?

更新

对下一个问题:

  ExecuteRestLogin executeRestLogin = new ExecuteRestLogin();
// The below execute method works, but I cannot get the result of postExecute in it, it shows me its not of the type Void, Void, String. 
 String result =  executeRestLogin.execute();

private class ExecuteRestLogin extends AsyncTask<Void,Void,String>{

        @Override
        protected String doInBackground(Void... params) {
            HttpHeaders requestHeaders = new HttpHeaders();
            requestHeaders.add("Cookie", "JSESSIONID=" + StaticRestTemplate.jsessionid);
            HttpEntity<String> requestEntity = new HttpEntity<>(requestHeaders);

            HttpEntity<String> rssResponse = rest.exchange(
                    "http://192.168.178.60:8080/dashboard",
                    HttpMethod.GET,
                    requestEntity,
                    String.class);
            StaticRestTemplate.jsessionid = rssResponse.getBody();
            return StaticRestTemplate.jsessionid;
        }

        @Override
        protected void onPostExecute(String aVoid) {
            super.onPostExecute(aVoid);
        }
    }

2 个答案:

答案 0 :(得分:0)

您应该用执行()替换 doInBackground()。有了这个,doInBackground方法中的代码将在另一个线程中执行。(你不应该创建一个新的线程,Android会为你做。)

答案 1 :(得分:0)

更改您的代码,如:

private class ExecuteRestLogin extends AsyncTask<Void,Void,Boolean> {
 @Override
    protected Boolean doInBackground(Void... params) {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Cookie", "JSESSIONID=" + StaticRestTemplate.jsessionid);
        HttpEntity<String> requestEntity = new HttpEntity<>(requestHeaders);

        HttpEntity<String> rssResponse = rest.exchange(
                "http://192.168.178.60:8080/dashboard",
                HttpMethod.GET,
                requestEntity,
                String.class);
        StaticRestTemplate.jsessionid = rssResponse.getBody();
        result = StaticRestTemplate.jsessionid; // result is a global String.
        return true;
    }

 @Override
    protected void onPostExecute(final Boolean success) {
         if (success) {
            //You can use your result string now.
        }
    }
}