我使用Volley从JSON Array获得响应。我得到了我需要的琴弦。 我想将这些字符串放在ArrayList中,以便将数据放入recyclerview适配器中。但是,这种情况并没有发生。
这是Json请求:
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mRecyclerViewAdapter);
RequestQueue queue = VolleySingleton.getInstance(this).getRequestQueue();
String url ="http://www.drunkenwords.in/web.php";
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONArray jsonArray;
ArrayList<Model> resp = new ArrayList<>();
try {
jsonArray = response.getJSONArray("info");
String x= response.getString("status");
Toast.makeText(MainActivity.this,x,Toast.LENGTH_LONG).show();
for(int i=0; i<jsonArray.length(); i++){
resp.add(new Model(jsonArray.getString(i)));
Toast.makeText(MainActivity.this,resp.get(i).getTitle(),Toast.LENGTH_LONG).show();
}
mRecyclerViewAdapter.setItems(resp);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("y", "Error: " + error.getMessage());
}
});
queue.add(req);
mRecyclerViewAdapter.setItems(ar);
mRecyclerViewAdapter.notifyDataSetChanged();
请帮忙!
答案 0 :(得分:0)
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
RequestQueue queue = VolleySingleton.getInstance(this).getRequestQueue();
String url ="http://www.drunkenwords.in/web.php";
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONArray jsonArray;
ArrayList<Model> resp = new ArrayList<>();
try {
jsonArray = response.getJSONArray("info");
String x= response.getString("status");
Toast.makeText(MainActivity.this,x,Toast.LENGTH_LONG).show();
for(int i=0; i<jsonArray.length(); i++){
resp.add(new Model(jsonArray.getString(i)));
Toast.makeText(MainActivity.this,resp.get(i).getTitle(),Toast.LENGTH_LONG).show();
}
mRecyclerViewAdapter.setItems(resp);
mRecyclerView.setAdapter(mRecyclerViewAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("y", "Error: " + error.getMessage());
}
});
queue.add(req);
mRecyclerViewAdapter.setItems(ar);
mRecyclerViewAdapter.notifyDataSetChanged();
答案 1 :(得分:0)
尝试使用此
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("info");
for(int i = 0; i < jsonArray.length(); i++)
{
JSONObject jobj = jsonArray.getJSONObject(i);
String x= jobj.getString("status");
Toast.makeText(MainActivity.this,x,Toast.LENGTH_LONG).show();
resp.add(new Model(jsonArray.getString(i)));
Toast.makeText(MainActivity.this,resp.get(i).getTitle(),Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Log.e("exception", "Error parsing data " + e.toString());
}
希望这会对你有所帮助。
答案 2 :(得分:0)
Try This
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mRecyclerViewAdapter);
RequestQueue queue = VolleySingleton.getInstance(this).getRequestQueue();
String url ="http://www.drunkenwords.in/web.php";
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONArray jsonArray;
ArrayList<Model> resp = new ArrayList<>();
try {
jsonArray = response.getJSONArray("info");
String x= response.getString("status");
Toast.makeText(MainActivity.this,x,Toast.LENGTH_LONG).show();
for(int i=0; i<jsonArray.length(); i++){
resp.add(new Model(jsonArray.getString(i)));
Toast.makeText(MainActivity.this,resp.get(i).getTitle(),Toast.LENGTH_LONG).show();
}
mRecyclerViewAdapter.setItems(resp);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("y", "Error: " + error.getMessage());
}
});
queue.add(req);
mRecyclerViewAdapter.notifyDataSetChanged();
mRecyclerViewAdapter.setItems(ar);