MySQLの文を使用して必要なすべてのレコードを正常に選択できます。フォーマットは以下の通りです:AndroidからJSONを使用してデータベースからサブ配列を受け取る
{"result[
{"totalDis":"764.108207","totalTime":"36","totalCalories":"49.9464197344328","Weight":"65","Sex":"M","Age":"18","MaxSpeed":"44.16","LongestT":"00:00:10","FarthestDis":"144.7902",
"result2":[{"speed":"6.00","distance":"12"},
{"speed":"9.00","distance":"27"},
{"speed":"14.48","distance":"144.7902"},
{"speed":"1.45","distance":"10.135314"},
{"speed":"1.95","distance":"140.6154"},
{"speed":"44.16","distance":"88.322022"}
]}
]}
は、私はすでに以下のコードを使用して、データベースから多くのデータを受け取っているが、それらはすべて固有の名前を持っているように、私が受け取ったデータを定義することができますので、私は彼らが彼らに基づいて得ることができます名。しかしresult2では、同じフィールドに複数のデータがあり、それらをすべて取得して別の変数に格納したいと考えています。
private void getData(){
String url = Config.DATA_URL+user;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Toast.makeText(MyProfile.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String totalDis="";
String totalTime="";
String totalCalories="";
String weight;
String sex;
String age="";
String maxSpeed="";
String longestT="";
String farthestDis="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
totalDis = collegeData.getString(Config.KEY_TOTALDIS);
totalTime = collegeData.getString(Config.KEY_TOTALTIME);
totalCalories = collegeData.getString(Config.KEY_TOTALCALORIES);
weight = collegeData.getString(Config.KEY_Weight);
sex = collegeData.getString(Config.KEY_Sex);
age = collegeData.getString(Config.KEY_Age);
maxSpeed = collegeData.getString(Config.KEY_MaxSpeed);
longestT = collegeData.getString(Config.KEY_LongestT);
farthestDis = collegeData.getString(Config.KEY_FarthestDis);
double tDis = Double.parseDouble(totalDis);
tDis= Math.round(tDis);
String ttDis = String.valueOf(tDis);
double tCa = Double.parseDouble(totalCalories);
tCa= Math.round(tCa);
String ttCa = String.valueOf(tCa);
double fDis = Double.parseDouble(farthestDis);
fDis= Math.round(fDis);
String ffDis = String.valueOf(fDis);
double ms = Double.parseDouble(maxSpeed);
ms= Math.round(ms);
String mms = String.valueOf(ms);
txtUserDetail.setText("Weight: " + weight + "kg\nSex: "+sex + "\nAge: "+age);
txtTotalTime.setText("Total Distance: "+ttDis+"m\nTotal Time: "+totalTime+"s\nTotal Calories: "+ttCa);
txtX.setText("Fastest Speed: "+mms+"m/s\nLongest Duration: "+longestT+"\nFarthest Distance: "+ffDis+"m");
}
catch (JSONException e){
e.printStackTrace();
}
Config.class
public class Config {
public static final String DATA_URL = "http://10.1.1.1/display.php?user=";
public static final String KEY_TOTALDIS = "totalDis";
public static final String KEY_TOTALTIME = "totalTime";
public static final String KEY_TOTALCALORIES = "totalCalories";
public static final String KEY_Weight = "Weight";
public static final String KEY_Sex = "Sex";
public static final String KEY_Age = "Age";
public static final String KEY_MaxSpeed = "MaxSpeed";
public static final String KEY_LongestT = "LongestT";
public static final String KEY_FarthestDis = "FarthestDis";
public static final String JSON_ARRAY = "result";
}
を対応であなたをJSON結果を盗んであろうその2つの値を格納するモデルクラスを作成し、それらのオブジェクトで配列を埋めなければなりません。 – Nanoc
これをどのように行うことができますか? thz –
@ namnam0107 'gson'ライブラリを使ってこの配列を解析できます。応答を手動で解析するよりもずっと簡単です。 –