我收到以下错误:
这是我的源代码:
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中获取数据并将其绘制在图表上的片段。问题是当我显示&#34;数据&#34;在textView上具有JSON数据的命名变量然后它正确显示但是当我分配相同的&#34;数据&#34;变量到JSONObject然后发生上述错误。
这是我需要在线图上绘制的Json数据
[{"twitter":"200","googleplus":"60"},{"twitter":"150","googleplus":"180"},{"twitter":"90","googleplus":"120"}]
有人请告诉我如何获取JSON数据并对其进行解析并将其绘制在折线图上。
答案 0 :(得分:0)
尝试这个。
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);
}
答案 1 :(得分:0)
试试这种方式
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();
}