0

I'm getting stuck with http request using HttpClient that is working fine with 2.2 or 2.3.X versions. But it is giving me 401 error when I will tried to send that request from my android tablet with version 4.0.3

Here is my code that I have implemented.

HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
    HttpResponse response;
    JSONObject json = new JSONObject();
    try{
        HttpPost post = new HttpPost("MYURL");
        json.put("username", username);
        json.put("password", password);
        StringEntity se = new StringEntity( json.toString());  
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        post.setEntity(se);
        response = client.execute(post);
        /*Checking response */
        statusCode = response.getStatusLine().getStatusCode();
        System.out.println("Status Code=>" + statusCode);
        if (statusCode == 200) {
            result = EntityUtils.toString(response.getEntity());
            Log.v("Login Response", "" + result);
        } else {
            response = null;
        }
    }
    catch(Exception e){

        e.printStackTrace();
        //createDialog("Error", "Cannot Estabilish Connection");
    }

Help me to solve this problem with your best suggestions.

Thanks,

Jai
  • 1,974
  • 2
  • 22
  • 42

2 Answers2

2
I'm getting stuck with http request using HttpClient that is working fine with 2.2 or 2.3.X versions.

I have a doubt on NetworkOnMainThread Exception.

Look at How to fix android.os.NetworkOnMainThreadException?

Android AsyncTask is the best solution for it.

Update:

Also You are getting 401 error Status Code.

401 means "Unauthorized", so there must be something with your credentials.

Just check the Credential before requesting Web Service.

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • Yes exactly,I have thought about it. But How can I solve it? can you help me please? – Jai Jan 04 '13 at 10:42
  • 1
    Dear Expert, is this answer? Yes we know this is the answer, but there are so many thread already exists with the same words. – Paresh Mayani Jan 04 '13 at 10:42
  • obviously not, as 1/ there is no evidence of an exception being thrown. 2/ there is even an answer from the server (401). – njzk2 Jan 04 '13 at 10:44
  • All buddies, thanks for your suggestions. I think I have already put it on AsyncTask with method declaration. But don't Know let me check it again. – Jai Jan 04 '13 at 10:49
  • @user370305 : Hey, I have already used async :) I m still getting issue :) – Jai Jan 04 '13 at 11:26
  • @user370305 : Nothing in error stacktrace just its giving me statuscode 401 – Jai Jan 04 '13 at 11:40
  • `HTTP 401 response code stands for unauthorized request`. Also look at http://www.checkupdown.com/status/E401.html – user370305 Jan 04 '13 at 11:54
  • @user370305 : I have created simple demo with async and I'm passing the right username and password as it is working fine with verison 2.2 or 2.3.x. It is giving me code 200 it means Ok with that versions. I'm only getting 401 with version 4.0.3 :) – Jai Jan 04 '13 at 12:06
  • @user370305 : I'm also getting stuck with that reason :) – Jai Jan 04 '13 at 12:06
  • @user370305 : That is the stuck :) Its only behaving strange with version 4.0.3 – Jai Jan 04 '13 at 12:13
  • Means put some print of **username** and **password** also put print for complete **url** before making `http.execute()`. – user370305 Jan 04 '13 at 12:15
1

You're running a network operation on main thread. Use async task to run network operations in background thread. That's why you are getting android.os.NetworkOnMainThreadException.

do it in an async task like this:

class MyTask extends AsyncTask<String, Void, RSSFeed> {

    protected void onPreExecute() {
        //show a progress dialog to the user or something
    }

    protected void doInBackground(String... urls) {
        //do network stuff
    }

    protected void onPostExecute() {
        //do something post execution here and dismiss the progress dialog
    }
 }

 new MyTask().execute(null);

Here are some tutorials for you if you don't know how to use async tasks:

Tutorial 1
Tutorial 2

Here is official docs

Miral Dhokiya
  • 1,720
  • 13
  • 26
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84