我正在尝试通过JSON解析来获取数据,但无法将数据提取到我的应用程序中。请帮助
这是我的获取数据链接
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.17//my_home/services/getcategorydata.php?city=Islamabad"); //YOUR PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
虽然这是另一个
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
最后
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
//dic json call
//then get data
//
JSONObject json = jArray.getJSONObject(i);
how fetch array of php
switch (i)
{
case 0:
{
Care_title.setText(json.getString("name"));
Care_minutz.setText(json.getString("rate") );
child_mintz_.setText(json.getString("hour"));
break;
}
So on....
而我的php获取数据是
{"type":"1","data":[{"name":"Delivery","title":"Delivery","rate":"500 RS","icon":"http:\/\/localhost\/services\/images\/category_icon\n\/delivery.png","publishtype":"PUBLISHED","timestamp":"2015-08-25 12:32:50"},{"name":"Care","title":"Care","rate":"400 RS","icon":"http:\/\/localhost\/services\/images\/category_icon\n\/care.png","publishtype":"PUBLISHED","timestamp":"2015-08-25 12:32:43"}],"title":"Message","message":"","_explicitType":null}
答案 0 :(得分:0)
JSONObject json=new JSONObject(response);
JSONArray dataArray=json.getJSONArray("data");
for(int i=0;i<dataArray.length();i++)
{
JSONObject data=dataArray.getJSONObject(i);
String name=data.getString("name");
}
答案 1 :(得分:0)
您可以使用Volley Library来解析数据。
private void JsonCall() throws JSONException {
// TODO Auto-generated method stub
String url = "http://192.168.0.17//my_home/services/getcategorydata.php?city=Islamabad";
JsonObjectRequest jsonobject = new JsonObjectRequest(Method.PUT, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject arg0) {
//Your Json Responce
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
Log.e("Error Code:", arg0 + "");
}
})
};
AppController.getInstance().addToRequestQueue(jsonobject, "UPDATETIMELINE");
}
创建一个应用程序调用AppController.java。