Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

Update

To the next problem in question :

  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);
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

executeRestLogin.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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...