未调用 onResponse 方法



因为没有调用onResponse,所以我没有设置适配器,因为我在onResponse方法中设置了adpater。当我通过浏览器访问网址时,我得到 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"}]

主要活动.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

设置适配器和回收器视图应该在 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) {
        }
    });

Request.Method.POST更改为Request.Method.GET。我想调用应该是 GET,因为您调用它以获取要显示的数据(并且因为您可以从浏览器调用它并获得响应(。

编辑:哦,是的,然后还有@ZeroOne报告的问题。

最新更新