应用正在运行,但无法从json获取数据

时间:2019-07-02 07:17:29

标签: java android 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)

1 个答案:

答案 0 :(得分:0)

最后我解决了我的错误,我在json文件上写了错误的代码,因此它没有加载代码