Android:即使在使用AsyncTask时,主线程网络也会异常

问题描述:

我正在处理一个Android项目,并且要进行登录,我正在与服务器进行通信以进行身份​​验证.即使我使用的是AsyncTask,我也收到了NetworkOnMain线程异常.这是为什么.这是我的代码:

I am working on an Android project and for Login I am communicating with the server for authentication. Even though I am using AsyncTask, I am getting NetworkOnMain thread exception. Why is that. Here is my code :

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;
        }
    }

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

What am I doing wrong. Also, how can I get custom objects back as response from async task?

更新

下一个问题:

  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);
        }
    }

executeRestLogin.doInBackground();

调用execute()在后台线程上执行异步任务.直接调用doInBackground()只会在当前线程上运行该方法.

Call execute() to execute an async task on a background thread. Calling doInBackground() directly just runs the method on your current thread.

如何从异步任务中获取自定义对象作为响应

how can I get custom objects back as response from async task

将处理异步任务结果的代码放在onPostExecute()中.它在UI线程上运行.

Put the code that deals with async task results in onPostExecute(). It gets run on the UI thread.