我有安卓应用。在那个应用程序中,我在服务器上发布了一些字符串数据并得到了一些响应。问题是,我正在接收 jsonstring 中的响应,但我想要 json 数组中的这些数据。尽管当我使用 JsonArrayRequest 时,它不允许在参数中使用 post 方法,然后我的 Web 服务无法正常工作。所以我坚持使用 StringRequest 并且服务工作正常,但完整的响应作为一个完整的字符串返回。所以我无法在我的列表视图中显示我的数据。那么如何解决这个问题呢?
这是我的代码:
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(NewSearchPeople.this, response, Toast.LENGTH_LONG).show();
// search1();
try {
JSONArray jsonArray = new JSONArray(response);
for(int i=0;i<jsonArray.length();i++){
JSONObject obj = jsonArray.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("fullname"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(obj.getString("location"));
movie.setGenre(obj.getString("Description"));
movie.setYear(obj.getInt("id"));
movieList.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(NewSearchPeople.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("fullname", "pooja");
return params;
}
};
这是json响应:
[
{
"id":"442",
"fullname":"xyz(18 yr)",
"image":"\/2017-02-0823:49:521486619389674.jpg",
"location":"lkn","Description":null
},
{
"id":"443",
"fullname":"abc(28 yr)",
"image":"\/2017-02-0823:51:381486619493934.jpg",
"location":"md","Description":null
},
{
"id":"444",
"fullname":"Arya(25 yr)",
"image":"\/2017-02-0823:52:251486619540695.jpg",
"location":"ud","Description":null
}
]