我在这里使用本指南:
http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
我把
System.out.println(error.getMessage());
在我的下面的代码中,这是它向我显示的错误:
01-13 18:53:21.425 1915-1915/com.app.dev.myapplication I/System.out: org.json.JSONException: Value {"server_response":[{"first_name":"bob","last_name":"john","user_name":"bobthebuilder","avatar":"stringOfURL"}]} of type org.json.JSONObject cannot be converted to JSONArray
01-13 18:53:21.425 1915-1915/com.app.dev.myapplication D/Volley: [1] 2.onErrorResponse: SearchActivity
这是我的JSON代码:
{"server_response":[{"first_name":"bob","last_name":"john","user_name":"bobthebuilder","avatar":"stringOfURL"}]}
这是我的代码:
public class SearchActivity extends AppCompatActivity {
String JSON_STRING,sq,js_username,js_first_name,js_last_name,js_avatar;
EditText et_sq;
private static final String json_user_search_url = "http://www.domain.com/string.php";
private static final String TAG = SearchActivity.class.getSimpleName();
private List<SearchResults> movieList = new ArrayList<SearchResults>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
listView = (ListView) findViewById(R.id.listViewSearch);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(json_user_search_url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONArray responseArray = response.getJSONArray("server_response");
JSONObject content = responseArray.getJSONObject(0);
SearchResults movie = new SearchResults();
movie.setUser_name(obj.getString("user_name"));
movie.setFirst_name(obj.getString("first_name"));
movie.setLast_name(obj.getString("last_name"));
movie.setAvatarUrl(obj.getString("avatar"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.getMessage());
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
}
答案 0 :(得分:0)
响应是一个JSON对象,“server_response”是唯一的值。 “server_response”本身是由JSONObject包装的JSONArray,因此您需要更改:
new JsonArrayRequest(json_user_search_url, new Response.Listener<JSONArray>() {
为:
new JsonObjectRequest(json_user_search_url, new Response.Listener<JSONObject>() {
以及覆盖onResponse:
@Override
public void onResponse(JSONObject response) {
然后您可以访问响应并获取“server_response”而不像您那样循环遍历内容:
@Override
public void onResponse(JSONObject response) {
JSONArray responseArray = response.getJSONArray("server_response");
JSONObject content = responseArray.getJSONObject(0);
String firstName = content.getString("first_name");
//get rest of the objects similarly...
}
编辑:在原始答案中犯了一个错误。 “server_response”是包含索引0处的JSON对象的JSONArray。您可以访问JSONObject并将其内容存储为对象,然后为其数据提取键/值对。