0

由于未调用onResponse,因为我在onResponse方法中设置了adpater,所以未设置适配器。当我去到url throught浏览器,我得到了PHP的响应作为JSON响应未调用onResponse方法

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

显示完整的logcat –

回答

0

一套适配器和recyclerview应该是while循环变化Request.Method.POST

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) { 

     } 
    }); 
0

Request.Method.GET。我想这个调用应该是一个GET,因为你调用它来获取数据来显示(因为你可以从浏览器调用它并获得响应)。

编辑:哦,是的,那么也有@ZeroOne报告的问题。