Catch中的陈述未被执行

时间:2017-06-07 12:56:59

标签: java android

我需要从我的网络服务中获取登录详细信息,以验证我的应用中的登录信息。以下是完成工作的代码。

      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;
        }

问题我面对的是我在logcat中看不到与之相关的任何内容但在调试时我发现控件转到了 } catch (MalformedURLException e) {
但是System.out.println("URLConnection Exception: " + e);
从未执行过。我是Android开发人员的新手,所以可能会有一些我无法看到的东西。请帮忙。

编辑 - 我首先尝试使用Log.e,但它没有工作,所以我放了System.out.println,但也没有。

2 个答案:

答案 0 :(得分:1)

你不应该使用System.out, 而是使用logcat等日志记录功能进行调试。

在此示例中,考虑到catch捕获错误,您应该使用:

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());
                }

以下是how to use them properly的解释。

如果您看不到logcat,请转到there

答案 1 :(得分:0)

试试

  

Log.e(“tag”,“Exception:”+ e);

你可以在android监视器中找到它