ANDROID-MPAndroidChart在json API上显示条形图
我有一本来自英国的教程 但我想用这个ANDROID-MPAndroidChart在json API上显示条形图,android,json,charts,Android,Json,Charts,我有一本来自英国的教程 但我想用这个jsonapi在条上显示值: [ { "id_dosen": "3", "id_matkul": "2", "nilai": "1", "jumlah": "18" }, { "id_dosen": "3", "id_matkul": "2", "nilai": "2", "jumlah": "2" }, { "id_dosen": "3", "id_matk
jsonapi
在条上显示值:
[
{
"id_dosen": "3",
"id_matkul": "2",
"nilai": "1",
"jumlah": "18"
},
{
"id_dosen": "3",
"id_matkul": "2",
"nilai": "2",
"jumlah": "2"
},
{
"id_dosen": "3",
"id_matkul": "2",
"nilai": "3",
"jumlah": "7"
},
{
"id_dosen": "3",
"id_matkul": "2",
"nilai": "4",
"jumlah": "6"
},
{
"id_dosen": "3",
"id_matkul": "2",
"nilai": "5",
"jumlah": "3"
}
]
我已尝试使用此代码来显示:
MainActivity.java
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(jsonStr);
} catch (JSONException e) {
e.printStackTrace();
}
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String jumlah = c.getString("jumlah");
String nilai = c.getString("nilai");
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("id", nilai);
contact.put("name", jumlah);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
但就我所知,我不知道如何更改public void AddValuesToBARENTRY()
,因为我的数据已经存储在联系人列表中
有人可以帮我修一下。你们知道怎么解决吗?求你了,伙计们,知道怎么解决吗?请