簡體   English   中英

應用程序正在運行但未從 json 獲取數據

[英]App is running but does not fetch data from json

我正在學習 json 而沒有得到解決方案。

我嘗試更新我的 json 文件和 xml 文件,但沒有工作。

這是代碼

我在代碼中遇到的錯誤是寫的

下面請幫助我完成它。

該應用程序運行良好,但無法從https://api.myjson.com/bins/1hkm17下面的鏈接中獲取數據

package com.taxsmart.jsonsimpleexample;

import android.os.AsyncTask;
import com.taxsmart.jsonsimpleexample.MainActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data ="";
    String dataParsed = "";
    String singleParsed ="";
    @Override
    protected Void doInBackground(Void... voids) {
    try {
            URL url = new URL("https://api.myjson.com/bins/1hkm17");
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while(line != null){
                line = bufferedReader.readLine();
                data = data + line;
            }

            JSONArray JA = new JSONArray(data);
            for(int i =0 ;i <JA.length(); i++){
                JSONObject JO = (JSONObject) JA.get(i);
                singleParsed =  "Name" + JO.get("name") + "\n"+
                                "Password" + JO.get("Password") + "\n"+
                                "Contact" + JO.get("Contact") + "\n"+
                                "Country" + JO.get("Country") + "\n";

                dataParsed = dataParsed + singleParsed +"\n" ;

            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        MainActivity.data.setText(this.dataParsed);

    }
}

錯誤在這里

I/System.out: [CDS]rx timeout:0
[CDS]rx timeout:0
[OkHttp] sendRequest>>
I/System.out: [OkHttp] sendRequest<<
W/System.err: org.json.JSONException: No value for Name
    at org.json.JSONObject.get(JSONObject.java:389)
W/System.err:     at 
com.taxsmart.jsonsimpleexample.fetchData.doInBackground
(fetchData.java:38)at 
 com.taxsmart.jsonsimpleexample.fetchData.doInBackground
 (fetchData.java:18)
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
    at 
  java.util.concurrent.ThreadPoolExecutor.runWorker
 (ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run
  (ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)

最后我解決了我的錯誤我在我的 json 文件上寫了錯誤的代碼,所以它沒有加載代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM