activity启动时开一个新线程去下载json数据,用来更新选项。
问题是当碰到url地址错误,wifi没有打开等等导致的异常,如何弹出一个对话框给用户提示?并且点击后关闭?
在catch中似乎不能用dialog和toast,会报类似Cannot refer to a non-final variable context inside an inner class defined in a different method这样的错误,即便context是fianl也无法显示。是不是和ui线程是非安全的有关系?
问的有点乱。。也刚刚接触这块。。代码如下。。各位同行不吝指教哈 HandlerThread thread = new HandlerThread("MyThread");
thread.start();
Handler myhandler = new Handler(thread.getLooper()){ @Override
public void handleMessage(Message msg) {
HttpGet request = new HttpGet("http://192.168.1.120/test/list.php");
HttpResponse httpResponse;
JSONObject result =null;
try {
httpResponse = new DefaultHttpClient().execute(request);
String retSrc = EntityUtils.toString(httpResponse.getEntity());
if(retSrc != null)
result = new JSONObject(retSrc);
}catch (JSONException e) {
showDialog(1);
}catch (Exception e) {
e.printStackTrace();
Log.e("HTTP",e.toString());
}
}
};
问题是当碰到url地址错误,wifi没有打开等等导致的异常,如何弹出一个对话框给用户提示?并且点击后关闭?
在catch中似乎不能用dialog和toast,会报类似Cannot refer to a non-final variable context inside an inner class defined in a different method这样的错误,即便context是fianl也无法显示。是不是和ui线程是非安全的有关系?
问的有点乱。。也刚刚接触这块。。代码如下。。各位同行不吝指教哈 HandlerThread thread = new HandlerThread("MyThread");
thread.start();
Handler myhandler = new Handler(thread.getLooper()){ @Override
public void handleMessage(Message msg) {
HttpGet request = new HttpGet("http://192.168.1.120/test/list.php");
HttpResponse httpResponse;
JSONObject result =null;
try {
httpResponse = new DefaultHttpClient().execute(request);
String retSrc = EntityUtils.toString(httpResponse.getEntity());
if(retSrc != null)
result = new JSONObject(retSrc);
}catch (JSONException e) {
showDialog(1);
}catch (Exception e) {
e.printStackTrace();
Log.e("HTTP",e.toString());
}
}
};
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货