Java Catch中的语句是';不执行
我需要从我的web服务获取登录详细信息,以验证我应用程序中的登录。下面是执行此任务的代码Java Catch中的语句是';不执行,java,android,Java,Android,我需要从我的web服务获取登录详细信息,以验证我应用程序中的登录。下面是执行此任务的代码 try { //Apache Libraries and namevaluepair has been deprecated since APK 21(?). Using HttpURLConnection instead. URL url = new URL(wsURL + authenticate);
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中没有看到任何与此相关的内容,但在调试时,我发现控件转到了}捕获(格式错误的异常异常e){
但是
System.out.println(“URLConnection Exception:+e);
从未执行过。
我是Android开发新手,所以可能有一些东西我看不到。请帮助 编辑-我第一次尝试使用
Log.e
,但它不起作用,所以我将System.out.println
放进去,它也不起作用。试试看
Log.e(“标记”,“异常:+e”)
您可以在android monitor中找到它您不应该使用System.out, 而是使用日志功能,如用于调试 在本例中,考虑到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());
}
下面是一个解释
如果看不到logcat,请转到。进行调试,将异常置于catch块内,并查看调试点是否位于catch块内使用
Log.e
而不是System.out.println
它确实位于catch块内,但从未执行print语句我尝试使用Log.e,但没有执行luck@AshvinSharma如果不是,你怎么知道它进入了捕捉区日志记录?确保您的android studio设置为收集正确的日志(无过滤)。否则,通过调用throw new IOException(“”),确保它确实出错
或其他什么我在调试器中看到它,它会转到catch block,如果你想的话我可以发布一张图片。但是我不知道最后一部分,投掷会有帮助吗?e.printStackTrace();
会更适合你尝试的e.printStackTrace()
不起作用。请在Log.e上设置断点,如果遇到断点,只需将logcat级别更改为“ERROR”我这样做了,我很惊讶我在那里没有看到任何与之相关的东西。如果你跟踪它并看到在catch scoop中输入的代码,那么这个异常已经记录下来了,你可以找到它,即使你可以用标签搜索,只要确保你已经将log cat设置到了适当的级别,如果仍然没有找到,那么可能是三个错误在您的IDE中,尝试清理和重建您的项目我知道如何使用日志,但它不起作用,因此我尝试使用System.out.println,但它不起作用第一个System.out在android studio编程中没有意义。您知道logcat在android中的位置吗?是的,它设置在调试过滤器上,但错误不会显示在Log.e(标记,“URLConnection Exception:”+e);
t检查其他链接Yeah没有筛选,并且选择了调试,但仍然没有显示