嗨大家都希望你做得很好 我的应用程序有问题,我使用下面的代码连接到某个地址(正如你在这里看到它的本地地址)我的问题是当我关闭我的wamp服务器,或者例如,如果我的应用程序无法连接到我之前提供的地址,我的应用程序会在特定时间后停止,并且会给我一个错误。 我想知道如何更改我的代码,当应用程序找不到地址(由于任何原因),我的应用程序给用户的消息而不是Keep stops.how我可以处理吗?httpURLConnection使我的应用程序停止
这里是我的代码
String json_url = "http://10.0.2.2/ss/1.php";
@Override
protected String doInBackground(Void...voids){
try{
URL url = new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while((json_string=bufferedReader.readLine())!=null){
stringBuilder.append(json_string+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
System.out.println("Deal With Wrong Address");
return null;
}
return null;
}
感谢您的帮助。
你有没有下面的答案的帮助? –