I'm using Volley library to get JSON. I can't load JSON using emulator which runs on Android 2.3, everything just fine with other versions. I have no ideas why it can't get JSON, especially on 2.3 devices.
我正在使用Volley库来获取JSON。我无法使用在Android 2.3上运行的模拟器加载JSON,其他版本的一切都很好。我不知道为什么它不能获得JSON,特别是在2.3设备上。
JSON Object request:
private void makeJsonObjectRequest() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
showpDialog();
final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
urlJsonObj, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
extractJSONArray(response.getJSONArray("Accommodation"), "accommodation");
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error: ", error.getCause().toString());
error.printStackTrace();
hidepDialog();
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
My Log:
12-01 18:37:53.821 848-848/com.miesto.meniu D/Error﹕ org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
12-01 18:37:53.821 848-848/com.miesto.meniu W/System.err﹕ com.android.volley.ParseError: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
12-01 18:37:53.821 848-848/com.miesto.meniu W/System.err﹕ at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:73)
12-01 18:37:53.821 848-848/com.miesto.meniu W/System.err﹕ at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:121)
12-01 18:37:53.821 848-848/com.miesto.meniu W/System.err﹕ Caused by: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
12-01 18:37:53.821 848-848/com.miesto.meniu W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:107)
12-01 18:37:53.821 848-848/com.miesto.meniu W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:158)
12-01 18:37:53.821 848-848/com.miesto.meniu W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:171)
12-01 18:37:53.821 848-848/com.miesto.meniu W/System.err﹕ at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:68)
12-01 18:37:53.821 848-848/com.miesto.meniu W/System.err﹕ ... 1 more
12-01 18:37:55.001 848-848/com.miesto.meniu D/dalvikvm﹕ GC_EXTERNAL_ALLOC freed 421K, 55% free 3092K/6791K, external 811K/1260K, paused 53ms
12-01 18:37:55.431 848-848/com.miesto.meniu W/KeyCharacterMap﹕ No keyboard for id -1
12-01 18:37:55.431 848-848/com.miesto.meniu W/KeyCharacterMap﹕ Using default keymap: /system/usr/keychars/qwerty.kcm.bin
Any ideas?
1 个解决方案
#1
1
Ok first of all use Gson library it will make your life easier:
好的,首先使用Gson库,它会让你的生活更轻松:
And then make POJO of your responses e.g.
然后为您的回复制作POJO,例如
public class Accommodation {
public String id;
public String icon;
public String name;
public String nameEn;
public String type;
public String fotoLinks;
public String address;
public String workHours;
public String description;
public String descriptionEn;
public String votes;
public String voters;
public String number;
public String email;
public String latitude;
public String longitude;
}
And now combine all pojo classes to match your server response:
现在结合所有pojo类来匹配您的服务器响应:
public class ServerResponse {
public List<Accommodation> Accommodation;
public List<CashMachines> Cash_machines; // assuming you have CashMachines as well like above
}
And now build a request:
现在建立一个请求:
Request<ServerResponse> request = new GsonRequest<ServerResponse>(Request.Method.GET,
url,AnalyticsData.class, new Response.Listener<ServerResponse>() {
@Override
public void onResponse(ServerResponse response) {
// user response here
Log.d(">>", response.Accommodation.size()+"");
}, errorListener);
and now use this request
.
现在使用此请求。
Here is GsonRequest
. Make sure to download gson jar file from here.
这是GsonRequest。确保从这里下载gson jar文件。
Please note that existing code will immediately start working with this url http://njreteazza.lt.ricina.serveriai.lt/info.php i.e. you don't even need to create pojo classes for all types that are returned just Keep adding pojo classes when you need them.
请注意,现有代码将立即开始使用此URL http://njreteazza.lt.ricina.serveriai.lt/info.php,即您甚至不需要为所有返回的类型创建pojo类。继续添加pojo你需要的课程。
#1
1
Ok first of all use Gson library it will make your life easier:
好的,首先使用Gson库,它会让你的生活更轻松:
And then make POJO of your responses e.g.
然后为您的回复制作POJO,例如
public class Accommodation {
public String id;
public String icon;
public String name;
public String nameEn;
public String type;
public String fotoLinks;
public String address;
public String workHours;
public String description;
public String descriptionEn;
public String votes;
public String voters;
public String number;
public String email;
public String latitude;
public String longitude;
}
And now combine all pojo classes to match your server response:
现在结合所有pojo类来匹配您的服务器响应:
public class ServerResponse {
public List<Accommodation> Accommodation;
public List<CashMachines> Cash_machines; // assuming you have CashMachines as well like above
}
And now build a request:
现在建立一个请求:
Request<ServerResponse> request = new GsonRequest<ServerResponse>(Request.Method.GET,
url,AnalyticsData.class, new Response.Listener<ServerResponse>() {
@Override
public void onResponse(ServerResponse response) {
// user response here
Log.d(">>", response.Accommodation.size()+"");
}, errorListener);
and now use this request
.
现在使用此请求。
Here is GsonRequest
. Make sure to download gson jar file from here.
这是GsonRequest。确保从这里下载gson jar文件。
Please note that existing code will immediately start working with this url http://njreteazza.lt.ricina.serveriai.lt/info.php i.e. you don't even need to create pojo classes for all types that are returned just Keep adding pojo classes when you need them.
请注意,现有代码将立即开始使用此URL http://njreteazza.lt.ricina.serveriai.lt/info.php,即您甚至不需要为所有返回的类型创建pojo类。继续添加pojo你需要的课程。