我正在开发一个Android应用程序,它向get some data
的服务器发送请求,但问题是当我丢失网络连接我的应用程序获取崩溃< / strong>即可。或者如果我忘记更改 IP地址,应用程序也会崩溃。我在 HTTPGET 的演员阵容中观察到了这个问题,而不是在 HTTPPOST 中。我还检查Network Availability
,但它仍然仍然崩溃。我想在我的应用程序丢失连接而不是崩溃时向用户显示Toast消息。我正在分享下面的snappet。有人可以检讨一下我解决我的问题。我不知道我哪里出错了
CODE:
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
cellphoneDate = params[0];
Log.e("Alert---Service","Calling Service now (: ");
//Calling service now
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(GETTING_ALERTS_URL + "/"
+ cellphoneDate);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isInternetPresent = cm.isConnected();
if(isInternetPresent)
{
httpEntity = httpResponse.getEntity();
try {
Serv_Response = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (Serv_Response != null) {
////////////////////////////new code for getting list ///////////////////
JSONObject jsonObj1 = new JSONObject(Serv_Response);
JSONArray alertName = jsonObj1.getJSONArray(TAG_NAME);
for (int i = 0; i < alertName.length(); i++) {
JSONObject c = alertName.getJSONObject(i);
String alert_title = c.getString(TAG_ALERT_TITLE);
Alerts alertObject = new Alerts();
alertObject.setAlertTitle(alert_title);
alertsList.add(alertObject);
}
}
} catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
}
return null;
}
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// Toast.makeText(getBaseContext(), "From Database :" + Serv_GettingQuiz_Response, Toast.LENGTH_LONG).show();
//String array[] = new String[size];
if(Serv_Response.equals("{\"DoitResult\":[]}"))
{
//DoNothing
}
for(int i = 0; i < alertsList.size() ; i++ )
{
Log.e("Alert---Serice", "POP-UP notification");
showNotification(alertsList.get(i).getAlertTitle(), "TAP for More Details", i);
flag = true;
// savingDate(Serv_GettingQuiz_Response);
}
}
logcat的:
06-23 23:53:53.625: W/System.err(8610): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.9 refused
06-23 23:53:53.635: W/System.err(8610): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
06-23 23:53:53.635: W/System.err(8610): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-23 23:53:53.645: W/System.err(8610): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-23 23:53:53.645: W/System.err(8610): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:367)
06-23 23:53:53.655: W/System.err(8610): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
06-23 23:53:53.655: W/System.err(8610): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)
06-23 23:53:53.655: W/System.err(8610): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)
06-23 23:53:53.665: W/System.err(8610): at com.smartclasss.alerts.MyService$DoInBackground.doInBackground(MyService.java:169)
06-23 23:53:53.675: W/System.err(8610): at com.smartclasss.alerts.MyService$DoInBackground.doInBackground(MyService.java:1)
06-23 23:53:53.675: W/System.err(8610): at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-23 23:53:53.675: W/System.err(8610): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-23 23:53:53.685: W/System.err(8610): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-23 23:53:53.685: W/System.err(8610): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-23 23:53:53.695: W/System.err(8610): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-23 23:53:53.695: W/System.err(8610): at java.lang.Thread.run(Thread.java:864)
06-23 23:53:53.695: W/System.err(8610): Caused by: java.net.ConnectException: failed to connect to /192.168.1.9 (port 80): connect failed: ENETUNREACH (Network is unreachable)
06-23 23:53:53.715: W/System.err(8610): at libcore.io.IoBridge.connect(IoBridge.java:114)
06-23 23:53:53.715: W/System.err(8610): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-23 23:53:53.715: W/System.err(8610): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-23 23:53:53.725: W/System.err(8610): at java.net.Socket.connect(Socket.java:873)
06-23 23:53:53.725: W/System.err(8610): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:125)
06-23 23:53:53.735: W/System.err(8610): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-23 23:53:53.735: W/System.err(8610): ... 14 more
06-23 23:53:53.735: W/System.err(8610): Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
06-23 23:53:53.755: W/System.err(8610): at libcore.io.Posix.connect(Native Method)
06-23 23:53:53.755: W/System.err(8610): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
06-23 23:53:53.755: W/System.err(8610): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-23 23:53:53.765: W/System.err(8610): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-23 23:53:53.765: W/System.err(8610): ... 19 more
答案 0 :(得分:0)
您需要在执行请求之前检查互联网连接。此外,如果执行请求有异常,不要打印出堆栈跟踪,您还需要停止处理请求。