因为我没有设置onResponse,所以我没有设置适配器,因为我在onResponse方法中设置了adpater。当我通过浏览器访问url时,我将php响应作为JSON响应
PHP响应是JSON响应
[{"id":"pic 1","title":"Index - 1"},{"id":"pic 2","title":"Index - 2"},
{"id":"pic 3","title":"Index - 3"},{"id":"pic 4","title":"Index - 4"},
{"id":"pic 5","title":"Index - 5"},{"id":"pic 6","title":"Index - 6"},
{"id":"pic 7","title":"Index - 7"},{"id":"pic 8","title":"Index - 8"},
{"id":"pic 9","title":"Index - 9"},{"id":"pic 10","title":"Index - 10"},
{"id":"pic 11","title":"Index - 11"},{"id":"pic 12","title":"Index - 12"}]
MainActivity.java
package com.example.home.galleryapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
ArrayList<Album> arrayList = new ArrayList<>();
RecyclerView recyclerView;
RecyclerAdapter recyclerAdapter;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
layoutManager = new GridLayoutManager(this, 2); // 2 columns
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true); // to improve the performance
Log.d("MainActivity", "Before Json");
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST,
Config.serv_url,(String)null,
new Response.Listener<JSONArray>(){
@Override
public void onResponse(JSONArray response){
int count = 0;
Log.d("MainActivity", "Inside JSON");
while (count < response.length())
{
try{
JSONObject jsonObject = response.getJSONObject(count);
arrayList.add(new Album(jsonObject.getString("id"), jsonObject.getString("title")));
count += 1;
Log.d("MainActivity", "JSONObject id = "+ jsonObject.getString("id"));
}
catch (JSONException e){
e.printStackTrace();
}
recyclerAdapter = new RecyclerAdapter(arrayList, MainActivity.this);
recyclerView.setAdapter(recyclerAdapter);
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
}
});
MySingleton.getmInstance(MainActivity.this).addToRequestQueue(jsonArrayRequest);
}
}
错误是:
05-26 12:27:34.104 27120-27120/com.example.home.galleryapp E/RecyclerView:
No adapter attached; skipping layout
答案 0 :(得分:1)
set adapter和recyclerview应该在while循环之外
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST,
Config.serv_url, (String) null,
new Response.Listener < JSONArray > () {
@Override
public void onResponse(JSONArray response) {
int count = 0;
Log.d("MainActivity", "Inside JSON");
while (count < response.length()) {
try {
JSONObject jsonObject = response.getJSONObject(count);
arrayList.add(new Album(jsonObject.getString("id"), jsonObject.getString("title")));
count += 1;
Log.d("MainActivity", "JSONObject id = " + jsonObject.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
break;
}
}
recyclerAdapter = new RecyclerAdapter(arrayList, MainActivity.this);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
答案 1 :(得分:0)
将Request.Method.POST
更改为Request.Method.GET
。我猜这个调用应该是一个GET,因为你调用它来显示数据(因为你可以从浏览器调用它并得到响应)。
编辑:哦,是的,然后还有@ZeroOne报道的问题。