0

我正在使用我的网络服务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)
4

3 回答 3

0

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

有趣的是,您从不调用connect()您的 URLConnection,是吗?你真的应该。;)

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

于 2013-01-30T08:23:23.357 回答
-1

first take like this public class Quesans extends 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 = "answ";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 answ1 = new ArrayList();TextView txtque;int counter = 1;

于 2013-01-30T11:18:24.503 回答
-1

在公共同步 String getJSONString(String url) {...... ...... return null; 您正在返回空值。在结尾也传递 sb.tostring

于 2013-01-30T08:21:17.940 回答