完全阅读我的答案,我已经用简单的方式解释了。
首先,如果您使用Volley不需要http helper类,请使用Volley的方法通过对象或数组获取JSON数据。
第二个POJO类最好用它。是的,这是正确的做法。
这里是获取JSON对象数据的源代码,它们是从POJO中抽出和存储的。
/** * 方法,使JSON对象请求,其中JSON响应开始wtih { * */ 私人无效makeJsonObjectRequest(){
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
"http://api.example.com", null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
// Parsing json object response
// response will be a json object
String name = response.getString("name");
String email = response.getString("email");
//POJO class to store
Person person = new Person();
person.name=name;
person.email=email;
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
你可以重复使用json模型在db或db模型中使用json – KOTIOS
它们的结构是非常不同的,并不是所有的JSON实体在DB模型中都是相同的。我不认为将DB模型与JSON实体混合是一个好主意,因为我认为这会导致应用程序逻辑混乱。 – redrom
为两者创建不同的pojo并没有什么坏处,或者如果是小的diffrance,那么你可以使用非json关键字的临时keywork – KOTIOS