Ошибка синтаксического анализатора JSON: значение null типа org.json.JSONObject $ 1 не может быть преобразовано в JSONObject - PullRequest
0 голосов
/ 16 мая 2018

Я получаю следующую ошибку:

enter image description here

Это мой исходный код:

package com.example.arpok.iomatic;


LineGraphSeries<DataPoint> series;
ArrayList<HashMap<String, String>> arrayListData;
public static String data;
TextView graphOneTV,graphTwoTV,graphThreeTV;
GraphView graph;
String x,y;
public Graph_Fragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_graph_, container, false);
}
HttpURLConnection connection;
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    graphOneTV = (TextView) getView().findViewById(R.id.TVGraph1);
    graphTwoTV = (TextView) getView().findViewById(R.id.TVGraph2);
    graphThreeTV = (TextView) getView().findViewById(R.id.TVGraph3);
    graph = (GraphView) getView().findViewById(R.id.graph1);
    graphDataClass gdc = new graphDataClass();
    gdc.execute();


}
public class graphDataClass extends AsyncTask<String ,String ,String>
{
    @Override
    protected String doInBackground(String... strings) {

        try {
            URL url = new URL("http://192.168.1.34/getGraphData.php?");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = connection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            while(line != null)
            {
                line = bufferedReader.readLine();
                data = data + line;
            }

            JSONObject jo = new JSONObject(data);

            for(int i=0;i<data.length();i++) {

                x = jo.getString("twitter");
                y = jo.getString("googleplus");

                System.out.print("x: "+x+"y: "+y);

                HashMap<String, String> tempHashMapData = new HashMap<>();

                tempHashMapData.put("twitter",x);
                tempHashMapData.put("googleplus",y);

                arrayListData.add(tempHashMapData);
            }


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


    protected void onPreExecute()
    {
        super.onPreExecute();
    }
    protected  void onPostExecute(String s  )
    {
        super.onPostExecute(s);
        graphOneTV.setText(data);

    }
}}

Код вышефрагмент, в котором мне нужно получить данные из JSON и нанести их на график.Дело в том, что когда я отображаю именованную переменную «data», в которой есть данные JSON, в textView, то он отображается правильно, но когда я назначаю ту же переменную «data» для JSONObject, возникает вышеуказанная ошибка.

Это мои данные Json, которые мне нужно отобразить на линейном графике

[{"twitter":"200","googleplus":"60"},{"twitter":"150","googleplus":"180"},{"twitter":"90","googleplus":"120"}]

Кто-нибудь, пожалуйста, скажите мне, как я могу получить данные JSON, проанализировать их и нанести на график на линейном графике.

Ответы [ 2 ]

0 голосов
/ 16 мая 2018

Попробуйте вот так

ArrayList<HashMap<String, String>> arrayListSocial = new ArrayList<>();

String string = "[{\"twitter\":\"200\",\"googleplus\":\"60\"},{\"twitter\":\"150\",\"googleplus\":\"180\"},{\"twitter\":\"90\",\"googleplus\":\"120\"}]";
    try {
        JSONArray jsonarray = new JSONArray(string);
        for (int i = 0; i < jsonarray.length(); i++) {
            JSONObject jsonobject = jsonarray.getJSONObject(i);
           String socailTwiter= jsonobject.getString("twitter");
            String socailGplus = jsonobject.getString("googleplus");

            Log.e("socailTwiter",socailTwiter);

            HashMap<String, String> hashMap = new HashMap<>();

            hashMap .put("twitter",socailTwiter);
            hashMap .put("googleplus",socailGplus );


            arrayListSocial.add(hashMap);
        }

    }catch (Exception e){
       e.printStackTrace();
    }
0 голосов
/ 16 мая 2018

попробуйте это.

 JSONArray jsonarray = new JSONArray(jsonStr);
 for (int i = 0; i < jsonarray.length(); i++) {
     JSONObject jsonobject = jsonarray.getJSONObject(i);
            x = jo.getString("twitter");
            y = jo.getString("googleplus");

            System.out.print("x: "+x+"y: "+y);

            HashMap<String, String> tempHashMapData = new HashMap<>();

            tempHashMapData.put("twitter",x);
            tempHashMapData.put("googleplus",y);

            arrayListData.add(tempHashMapData);
}
...