0

I was trying to run the following code to perform a login and it was working fine but I wanted to try to update it to work with HttpURLConnection

    HttpPost post = new HttpPost("url");
    HttpClient client = new DefaultHttpClient();
    try{
        JSONObject user = new JSONObject();

        JSONObject userInfo = new JSONObject();
        userInfo.put("password", password);
        userInfo.put("email", email);

        user.put("user", userInfo);

        StringEntity sEntity = new StringEntity(user.toString());

        post.setEntity(sEntity);
        post.setHeader("Accept", "application/json");
        post.setHeader("Content-type", "application/json");

        return client.execute(post);

    }catch(Exception e){
        return null;
    }

I made changes similar to the code post on the question bellow but the when I try to log in it would just stay in the same screen but if I closed the app it would come up logged in, since I store the token so that it remains in the logged in state. This means the connection was established.

I used this as a guide: POST request send json data java HttpUrlConnection

Is HttpUrlConnection slower or more resource intensive? Did I do something wrong? I did see a too much going on in the main thread in Android Studio, is that related? Any help or response is appreciated.

Community
  • 1
  • 1
  • Do you have the network operation running in a worker Thread, such as an AsyncTask? Also, take a look at this question and also the accepted answer: http://stackoverflow.com/questions/13911993/sending-a-json-http-post-request-from-android – Daniel Nugent May 27 '15 at 20:10
  • I'm using an AsyncTask. Will chaning from OutputStreamWriter to DataOutputStream decrease the time it takes for the task to complete? – Andres Cedar May 27 '15 at 20:56
  • HttpUrlConnection can be thought of just a bit more smartly managed HttpClient. Basically, it provides response caching and compression support in an easier way. You should mark that requests will be buffered by default (roughly, if you not set chunk size) so it may cause you a faulty way of monitoring progress. – stdout Nov 17 '15 at 15:56

0 Answers0