【问题标题】:Error while downloading JSON下载 JSON 时出错
【发布时间】:2013-01-30 08:08:30
【问题描述】:

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

【问题讨论】:

  • 你能发布你的 logcat 吗?
  • 我已附上日志。请检查一下!
  • @Infinity 这不是 logact,它是堆栈跟踪。您的代码没有在您的Log 调用中任何 运行吗? -- 你能指出哪一行是 SetupFeeds.java 第 69 行吗?

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


【解决方案1】:

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

有趣的是,你从来没有在你的 URLConnection 上调用connect(),是吗?你真的应该。 ;)

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

【讨论】:

  • 谢谢!我尝试添加connect(),它也无济于事。我已经附上了logcat。看看能不能帮到你!
【解决方案2】:

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

【讨论】:

  • sb 超出范围,伙计。
  • 一旦检查您是否收到回复。以及您在哪里保存响应,您传递的是哪个字符串。通过 JsonString(String url) 您正在传递空值。所以它给出了nullpointerException。否则,在 try 块中获取字符串变量以保存响应,并且在 try{} 块内部和外部仅传递该字符串变量
  • 仍然有一个没有异常日志的捕获,并且返回 null。我将从记录堆栈跟踪开始
  • 一旦你检查了 sb.tostring。因为您正在传递 sb.tostring() 值或空值
  • @njzk2 请告诉我你在哪里看到没有?日志的catch?在代码中。确实,return null 很烦人,但它只会在记录异常后发生。
【解决方案3】:

首先采取像这样的公共类 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;单选按钮 btn_practice2; RadioButton btn_practice3;RadioButton btn_practice4;private ProgressDialog pDialog; 私有字符串名称 = "";私有字符串 tid = ""; 私有字符串盖子 = "";私有字符串 gid = "";私有字符串 id = ""; 私有静态最终字符串 TAG_SUCCESS = "成功";私有静态最终字符串 TAG_GROUP = "组";私有静态最终字符串 TAG_QUES = "ques";私有静态最终字符串 TAG_QUESID = "id";私有静态最终字符串 TAG_ANSW = "answ";私有静态最终字符串 TAG_TOPICNAME = "tname";私有静态最终字符串 TAG_TOPICID = "topicid";私有静态最终字符串 TAG_LEVELID = "levelid";私有静态最终字符串 TAG_GROUPID = "groupid";私有 ArrayList ques1 = new ArrayList();私有ArrayList quesid = new ArrayList();private ArrayList answ1 = new ArrayList();TextView txtque;int counter = 1;

【讨论】:

  • next @Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_quesans);Intent i=getIntent(); name=i.getStringExtra(TAG_TOPICNAME);tid=i.getStringExtra(TAG_TOPICID);lid=i.getStringExtra(TAG_LEVELID);
  • name=i.getStringExtra(TAG_TOPICNAME);tid=i.getStringExtra(TAG_TOPICID);lid=i.getStringExtra(TAG_LEVELID);gid = i.getStringExtra(TAG_GROUPID);
  • System.out.println("结果:"+name);System.out.println("结果:"+tid); system.out.println("结果:"+lid); system.out.println("结果:" + gid);txtque=(TextView)findViewById(R.id.que_txt); TextView txtRadio=(TextView) findViewById(R.id.rdtxt); btn_practicerg=(RadioGroup)findViewById(R.id.rdgroup); btn_practice1=(RadioButton)findViewById(R.id.RB1); btn_practice2=(RadioButton)findViewById(R.id.RB2);
  • btn_practice3=(RadioButton) findViewById(R.id.RB3); btn_practice4 = (RadioButton) findViewById(R.id.RB4); new LoadQuestions().execute();Button nextQuestion = (Button) findViewById(R.id.nxt_btn); nextQuestion.setOnClickListener(new OnClickListener(){ public void onClick(View v) { if (counter
  • 类 LoadQuestions 扩展 AsyncTask { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(Quesans.this); pDialog.setMessage("正在加载问题。请稍候..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); pDialog.show(); }
猜你喜欢
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多