下载JSON时出错

时间:2013-01-30 08:08:31

标签: android json amazon-web-services android-asynctask

我正在使用我的网络服务在我的应用中下载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;
}

logcat的:

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)

3 个答案:

答案 0 :(得分:0)

AWS很少失败。您是否配置了健康检查和通知?可能不是,因为你问这个问题 - 为什么不设置这个?

有趣的是,你永远不会在你的URLConnection上调用connect(),对吗?你真的应该。 ;)

大多数情况下,在NullPuinterException之前,您的代码应该能够生成消息;你看到哪一个?

答案 1 :(得分:-1)

在public synchronized String中getJSONString(String url) {...... ...... return null; 你正在返回null值。传递sb.tostring结尾也是

答案 2 :(得分:-1)

首先采取像这样的公共类,Quesans扩展Activity {JSONParser jsonParser = new JSONParser(); ArrayList questionDatas = new ArrayList(); JSONObject json; JSONArray groups = null; int i,k = 0; RadioGroup btn_practicerg; RadioButton btn_practice1; RadioButton btn_practice2; RadioButton btn_practice3; RadioButton btn_practice4; private ProgressDialog pDialog; private String name =“”; private String tid =“”; private String lid =“”; private String gid =“”; private String id =“”; private static final String TAG_SUCCESS =“success”; private static final String TAG_GROUP =“group”; private static final String TAG_QUES =“ques”; private static final String TAG_QUESID =“id”; private static final String TAG_ANSW =“answers”; private static final String TAG_TOPICNAME =“tname”; private static final String TAG_TOPICID =“topicid”; private static final String TAG_LEVELID =“levelid”; private static final String TAG_GROUPID =“groupid”; private ArrayList ques1 = new ArrayList(); private ArrayList quesid = new ArrayList(); private ArrayList answers1 = new ArrayList(); TextView txtque; int counter = 1;