0

I need to fetch login details from my web service to authenticate login in my app. Below is the code which does the job.

      try {
                //Apache Libraries and namevaluepair has been deprecated since APK 21(?). Using HttpURLConnection instead.
                URL url = new URL(wsURL + authenticate);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

                OutputStream os = connection.getOutputStream();
                os.write(postParam.getBytes());
                os.flush();
                os.close();

                // Fetching the response code for debugging purposes.
                int responseCode = connection.getResponseCode();
                Log.d(TAG, "POST Response Code: " + responseCode);

                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String inputLine;
                    StringBuilder response = new StringBuilder();

                    //Adding every responseCode in the inputLine.
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();

                    Log.d(TAG, "HTTP Response: " + response.toString());
                    //TODO: Check login logic again
                    //Sets Authentication flag
                    if (!response.toString().contains("Authentication Failed!")) {
                        authFlag = true;
                        Log.i(TAG, "Authentication Completed: Logged in Successfully!");
                        try {
                            JSONObject jsonObject = new JSONObject(response.toString());
                            JSONArray jsonArray = jsonObject.getJSONArray("rows");
                            JSONObject beanObject = jsonArray.getJSONObject(0);

                            userBean = new UserBean(username, beanObject.getString("User_FullName"), beanObject.getInt("UserType_Code"));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    Log.e(TAG, "Error!!! Abort!!!");
                }
                connection.disconnect();
            } catch (MalformedURLException e) {
                System.out.println("URLConnection Exception: " + e);
            } catch (IOException e) {
                System.out.println("IOStream Exception: " + e);
            }

            return postParam;
        }

Issue I'm facing is I don't see anything related to it in my logcat but on debugging I find that the control goes to
} catch (MalformedURLException e) {
but
System.out.println("URLConnection Exception: " + e);
is never executed.
I'm novice at Android dev so there might be something which I can't see. Please help.

EDIT - I first tried with Log.e but it didn't work so I put System.out.println which didn't work either.

Ashvin Sharma
  • 563
  • 1
  • 5
  • 24
  • For debugging put exception and see if debug point goes inside catch block – gati sahu Jun 07 '17 at 13:02
  • 1
    use `Log.e` instead of `System.out.println` – sestus Jun 07 '17 at 13:02
  • It does go inside the catch block but the print statement is never executed I tried with Log.e but no luck – Ashvin Sharma Jun 07 '17 at 13:07
  • @AshvinSharma How do you know it goes in the catch block if its not logging? Make sure your android studio is set to collect the correct logs(no filtering). Else ensure it does actually error, by calling `throw new IOException("");` or something – IAmGroot Jun 07 '17 at 13:15
  • I saw it in the debugger it goes to catch block, I can post a picture if you want. I don't get the last part though, is throwing gonna help?. – Ashvin Sharma Jun 07 '17 at 13:21

2 Answers2

1

You should not use System.out, instead use logging functions like logcat for debugging.

In this example, considering that the catch catches an error, you should use:

private static final String TAG = "MyActivity";
...
catch (MalformedURLException e) {
Log.e(TAG, "URLConnection Exception: " + e.getMessage());
                } catch (IOException e) {
Log.e(TAG, "IO error: " + e.getMessage());
                }

Here is an explanation of how to use them properly.

If you can't see logcat, go there.

Thecave3
  • 762
  • 12
  • 27
0

just try

Log.e("tag","Exception: " + e);

and you can find it in android monitor

  • 1
    `e.printStackTrace();` Would be more ideal to what you are trying – IAmGroot Jun 07 '17 at 13:05
  • `e.printStackTrace();` didn't work either – Ashvin Sharma Jun 07 '17 at 13:10
  • put a break point on Log.e and if it hit you just need to change the logcat level to "ERROR" – Naser Khoshfetrat Jun 07 '17 at 13:18
  • I did that, and I'm surprised I saw nothing there related to it. – Ashvin Sharma Jun 07 '17 at 16:57
  • if you traced it and saw code entered inside of catch scoop , then this exception has logged and you can find it , even you can search with tag just make sure you have set log-cat to proper level , and if still it doesn't found , with less possibility may be threes something wrong with your IDE , try to clean and rebuild your project – Naser Khoshfetrat Jun 07 '17 at 18:18
  • Yeah I bet there is something wrong either with the IDE or with the emulator, my logcat is full of irrelevant `com.google.android.gms.persistent` errors but not the one I want. I checked all the level Verbose, Debug, Warnings, Errors. I even tried searching but no sign of it. – Ashvin Sharma Jun 08 '17 at 06:13