Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android JSONObject超时_Android_Json - Fatal编程技术网

Android JSONObject超时

Android JSONObject超时,android,json,Android,Json,因此,每次启动应用程序时,我都使用JSONObject从服务器获取一些值。问题是,如果服务器停机或运行缓慢,应用程序就会停止。克服这个问题的最好方法是什么 List<NameValuePair> params = new ArrayList<NameValuePair>(); JSONObject json = jParser.makeHttpRequest(url, "GET", params); try { int success = json.getI

因此,每次启动应用程序时,我都使用JSONObject从服务器获取一些值。问题是,如果服务器停机或运行缓慢,应用程序就会停止。克服这个问题的最好方法是什么

List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url, "GET", params);
  try {
     int success = json.getInt(TAG_SUCCESS);
     //it stops here
     if (success == 1) { ... }
  } catch (JSONException e) { e.printStackTrace(); }
List params=new ArrayList();
JSONObject json=jParser.makeHttpRequest(url,“GET”,参数);
试一试{
int success=json.getInt(TAG_success);
//到此为止
如果(成功==1){…}
}catch(JSONException e){e.printStackTrace();}

1-如果可能,您可以在后台进行设置,并在完成后进行更新


2-如果您需要启动应用程序的信息,您可以在加载数据时合理设置连接超时和快速启动屏幕。

这更多的是设计问题,而不是编程问题。如果服务器关闭,则没有代码将其恢复;)

因此,考虑一下在脱机模式下工作的最佳方法,并实施它

  • 使用缓存数据
  • 通知用户没有连接
  • 考虑使用Android.forceServerOnline()

在本地缓存响应,并且仅在更新失败时使用旧数据每隔一段时间更新一次。您不应该在try/catch中包含
makeHttpRequest
,以防发生意外情况,例如,出现超时异常吗?