1

I just got recently involved with android I have this code, I have it commented for some info.

 HttpURLConnection con = null;
                    String token = null;
                    String message = null;
                    try {


                        con = (HttpURLConnection)new URL(Constants.URL_LOGIN + deviceID).openConnection();
                        con.setDoOutput(true);
                        con.setUseCaches(false);
                        con.setRequestMethod("POST");
                        con.setRequestProperty("Accept", "application/json");

                        DataOutputStream dos = new DataOutputStream(con.getOutputStream());

                        System.out.println("pin wala" + username + password + pincode);
                        dos.write((Constants.JSON_LOGIN_USERNAME + "=" + username + "&" + Constants.JSON_LOGIN_PASSWORD + "=" + password).getBytes(Charset.forName("UTF-8")));


                        dos.close();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        StringBuilder jsonStringBuilder = new StringBuilder();
                        String s = null;
                        while((s = reader.readLine()) != null) {
                            jsonStringBuilder.append(s);
                            System.out.println(s);
                        }

                        reader.close();

                        JSONObject jsonLogin = new JSONObject(jsonStringBuilder.toString());

                        if(jsonLogin != null) {
                                //here, should go the the return

                                //if statuscode is 401 i have this mystatus to be setted if not the api call is successful

                            if(jsonLogin.getInt("mystatus") == 402){

                                MainActivity.this.forPincode = true;
                            } else {

                                // no problem here this is successful api call 
                            }

                        }


                    } catch(MalformedURLException e){
                        e.printStackTrace();
                    } catch (IOException e) {

                        e.printStackTrace();
                    } catch(JSONException e){
                        e.printStackTrace();
                    }
                    finally {
                        if(con != null)
                            con.disconnect();
                    }

this works fine if I supply correct username and password but if not the http response code is 401 it throws an IOException java.io.IOException: No authentication challenges found and I couldn't get my response

this is my expected response when i get 401

{
    "status": "error",
    "message": "",
    "data": [],
    "status_code": 402
}

could anyone help me I have this task whole day and i couldn't seem to finish. any comment or suggestion would do. Thanks in advance

CaffeineShots
  • 2,172
  • 7
  • 33
  • 58
  • `getInt("mystatus") == 402.1` are you sure about this? – njzk2 Jun 08 '15 at 14:49
  • @njzk2 hi, I edit my post to be direct on issue about getting response if i get 401. But yeah I'm not sure about that since I haven't been that far. I couldn't get my response code so. but thanks for pointing that out. – CaffeineShots Jun 08 '15 at 14:52

1 Answers1

4

con.getInputStream() works only if Http returns a code between 200 and 299. For any other response code use con.getErrorStream().

You could should look like this:

BufferedReader reader = new BufferedReader(new InputStreamReader(con.getResponseCode() / 100 == 2 ? con.getInputStream() : con.getErrorStream()));
azertiti
  • 3,150
  • 17
  • 19
  • but if i use getErrorStream I should be able to get 200 return? – CaffeineShots Jun 08 '15 at 14:47
  • I edited the response. Basically you'll use getInputStream() for 2xx responses and getErrorStream() for everything else. – azertiti Jun 08 '15 at 14:53
  • still getting `java.io.IOException: No authentication challenges found` – CaffeineShots Jun 08 '15 at 14:58
  • Looks like that is a second issue - you should still use the code I provided otherwise you'll fail on any response with code >= 300. For your I/O exception check this http://stackoverflow.com/questions/17121213/java-io-ioexception-no-authentication-challenges-found – azertiti Jun 08 '15 at 15:03
  • @azertiti hi the issue for `no authentication challenges found` is fixed by that link. Also the code for reading error stream work fine using the code above. – CaffeineShots Jun 10 '15 at 07:26