GSONライブラリとVolleyを使用してJSON
以下を解析する方法GSONでJSONを解析してVolleyを使用してエラーを取得する
[
{
"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リクエストで、Iデータを解析することはできますが、データがGSONによってPOJOクラスに設定されているときは問題が発生します。
以下は、私のJSON
のVolleyライブラリを使用した解析です。
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)
GSONすることにより、このJSONを解析するために私を助けてください。 ありがとう
エラーが発生した場合は、エラーログも追加してください。 –
エラーログを追加しました。 /:@AminMousavi –
私は[\ _objectをBEGINが、行1、列2(少し編集)で\ _arrayをBEGINた予想](HTTPSこれはあなたを助けることができるhttps://stackoverflow.com/a/9598988/2940733の –