通过GSON使用Volley获取错误来解析JSON
如何使用GSON库和Volley解析低于JSON
。通过GSON使用Volley获取错误来解析JSON
[
{
"code": 1,
"message": "Scan Details",
"result": [
{
"Inbound Date": "2017-10-13",
"Outobund Date": "2017-10-16",
"Inbound": "3",
"Outbound": "3",
"Outbound Pending": "0"
},
{
"Inbound Date": "2017-10-16",
"Outobund Date": "2017-10-16",
"Inbound": "3",
"Outbound": "2",
"Outbound Pending": "1"
}
]
}
]
我已经创建了POJO类:
DashboardPojo_BK
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.json.JSONObject;
public class DashboardPojo_BK implements Serializable {
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("result")
@Expose
private List<ResultDashboard_BK> result = null;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<ResultDashboard_BK> getResult() {
return result;
}
public void setResult(List<ResultDashboard_BK> result) {
this.result = result;
}
}
ResultDashboard_BK
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
public class ResultDashboard_BK implements Serializable {
@SerializedName("Inbound Date")
@Expose
private String inboundDate;
@SerializedName("Outobund Date")
@Expose
private String outobundDate;
@SerializedName("Inbound")
@Expose
private String inbound;
@SerializedName("Outbound")
@Expose
private String outbound;
@SerializedName("Outbound Pending")
@Expose
private String outboundPending;
public String getInboundDate() {
return inboundDate;
}
public void setInboundDate(String inboundDate) {
this.inboundDate = inboundDate;
}
public String getOutobundDate() {
return outobundDate;
}
public void setOutobundDate(String outobundDate) {
this.outobundDate = outobundDate;
}
public String getInbound() {
return inbound;
}
public void setInbound(String inbound) {
this.inbound = inbound;
}
public String getOutbound() {
return outbound;
}
public void setOutbound(String outbound) {
this.outbound = outbound;
}
public String getOutboundPending() {
return outboundPending;
}
public void setOutboundPending(String outboundPending) {
this.outboundPending = outboundPending;
}
}
这是POST请求我现在用在这里,我可以解析数据,但是当数据由GSON设置为POJO类时会出现问题。
以下是我使用Volley库解析的JSON
。
String url = Constants.BLOOMKONNECT_DASHBOARD;
Log.e("URL",""+url);
StringRequest eventoReq = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("RESPONSE", response.toString());
utils.hideDialog();
try {
JSONArray j = new JSONArray(response);
// Parse a json
for (int i = 0; i < j.length(); i++) {
try {
JSONObject obj = j.getJSONObject(0);
String code = String.valueOf(obj.get("code"));
Log.e("CODE", "" + code);
if(code.equals("1")){
utils.showtoast("i m here");
Gson gson = new Gson();
Type listType = new TypeToken<ResultDashboard_BK>() {
}.getType();
ResultDashboard_BK pojo = gson.fromJson(response.toString(), listType);
Log.e("DATA",""+pojo);
} else {
utils.hideDialog();
utils.showtoast("Unexpected Response");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error: ", "" + error.getMessage());
//hidePDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", userId);
Log.e("USER_ID",""+userId);
return params;
}
};
// Añade la peticion a la cola
AppController.getInstance(this).addToRequestQueue(eventoReq);
的error_log
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.kabloom.kbinternal.activities.BloomKnctDashboard$3.onResponse(BloomKnctDashboard.java:142)
at com.kabloom.kbinternal.activities.BloomKnctDashboard$3.onResponse(BloomKnctDashboard.java:118)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.kabloom.kbinternal.activities.BloomKnctDashboard$3.onResponse(BloomKnctDashboard.java:142)
at com.kabloom.kbinternal.activities.BloomKnctDashboard$3.onResponse(BloomKnctDashboard.java:118)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
请帮我解析此JSON由GSON。 三江源
在您的代码中对此进行更改。
Gson gson = new Gson();
Type listType = new TypeToken<List<DashboardPojo_BK>>() {}.getType();
List<DashboardPojo_BK> mList = gson.fromJson(response.toString(), listType);
你的代码通过问题解决什么我越来越。 Thankyou –
你可以接受我的回答。@ Abhishekkumar谢谢。 – KeLiuyue
请帮助我多一点,我必须在RecyclerView中设置结果数据。 adapter = new InOutboundAdapter(this,mList.get(0).getResult()); recyclerView.setAdapter(adapter); 我必须发送arrayList到适配器。 –
添加这个编译的build.gradle文件相关
compile 'com.google.code.gson:gson:2.8.1'
然后让响应后,就可以解析JSON使用到GSON以下
Type listType = new TypeToken<List<DashboardPojo_BK>>(){}.getType();
List<DashboardPojo_BK> responseModel = (List<DashboardPojo_BK>) gson.fromJson(response, listType);
//其中效应初探是字符串你从api响应得到
得到同样的错误@Satender –
我不知道为什么根元素是JsonArray,但最好是因为JsonArray而对应循环。尝试下面的代码。
try {
JSONArray j = new JSONArray(response);
Gson gson = new Gson();
// Parse a json
for (int i = 0; i < j.length(); i++) {
JSONObject obj = j.getJSONObject(i);
DashboardPojo_BK pojo = gson.fromJson(obj.toString(), DashboardPojo_BK.class);
if (pojo.getCode().equals("1")){
utils.showtoast("i m here");
// do somthing with pojo
} else {
utils.hideDialog();
utils.showtoast("Unexpected Response");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
你JSON是一个JSONArray,所以你需要分析List<DashboardPojo_BK>
,而不是直接解析DashboardPojo_BK
。
如果您收到错误,请添加错误日志。 –
我添加了我的错误日志。 @AminMousavi –
我认为这可以帮助你https://stackoverflow.com/a/9598988/2940733 –