我有安卓应用程序。在该应用程序中,我在服务器上发布一些字符串数据并获得一些响应。
问题是,我收到 jsonstring 中的响应,但我希望此数据位于 json 数组中。
尽管当我使用 JsonArrayRequest 时,它不允许在参数中使用 post 方法,然后我的网络服务无法工作。
所以我坚持使用 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
}
]