我正在使用我的网络服务JSON
在我的应用程序中下载数据。但是,有时我无法下载JSON
数据并返回空值。我还必须提到我正在为我的服务器使用 Amazon Web Service。
由于有时数据下载没有错误并且有时会引发空异常,因此我很困惑它是否必须对有故障的 Web 服务或我的代码做某事。我正在下载数据AsyncTask
并使用下面给出的代码:
请帮帮我!
public synchronized JSONArray getJSONArrayFromUrl(String url) {
JSONArray jArray = null;
String json = getJSONString(url);
try {
jArray = new JSONArray(json);
} catch (Exception e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jArray;
}
public synchronized JSONObject getJSONObjectFromUrl(String url) {
JSONObject jObj = null;
String json = getJSONString(url);
try {
jObj = new JSONObject(json);
} catch (Exception e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
public synchronized String getJSONString(String url)
{
try {
URL url1 = new URL(url);
URLConnection tc = url1.openConnection();
tc.setConnectTimeout(timeout);
tc.setReadTimeout(timeout);
BufferedReader br = new BufferedReader((new InputStreamReader(tc.getInputStream())),8000);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
catch(Exception e)
{
Log.d("Error","In JSON downloading");
}
return null;
}
日志猫:
01-30 09:55:09.232: E/JSON Parser(18228): Error parsing data java.lang.NullPointerException
01-30 09:55:09.232: W/System.err(18228): java.lang.NullPointerException
01-30 09:55:09.232: W/System.err(18228): at com.ex.feeds.SetupFeeds.doInBackground(SetupFeeds.java:69)
01-30 09:55:09.232: W/System.err(18228): at com.ex.feeds.SetupFeeds.doInBackground(SetupFeeds.java:1)
01-30 09:55:09.232: W/System.err(18228): at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-30 09:55:09.232: W/System.err(18228): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-30 09:55:09.232: W/System.err(18228): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-30 09:55:09.232: W/System.err(18228): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-30 09:55:09.232: W/System.err(18228): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-30 09:55:09.232: W/System.err(18228): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-30 09:55:09.237: W/System.err(18228): at java.lang.Thread.run(Thread.java:856)