【问题标题】:GSON using Volley in Fragment - ErrorsGSON 在片段中使用 Volley - 错误
【发布时间】:2018-01-01 10:43:00
【问题描述】:

我正在尝试使用 Volley 在 Fragment 中使用 GSON 解析 JSON。但是我在两个地方遇到了编译时错误

  1. new Response.Listener<String> : 说 Listener 无法解析

  2. new Response.ErrorListener():说ErrorListener无法解析

  3. 无法解析方法“fromJson”

    public class Gson extends Fragment 
    {
    
        RecyclerView recyclerView;
        Response responseobj;
        CustomAdapter customAdapter;
        String url = "https://newsapi.org/v1/articles?source=techcrunch&apiKey=ed28d2d13805495a9b896ecb0c7b6ed1";
        Gson gson;
        RequestQueue requestQueue;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        {
    
            View view = inflater.inflate(R.layout.gson, container, false);
    
            recyclerView = (RecyclerView) view.findViewById(R.id.movielist);
    
            requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
    
            StringRequest stringRequest = new StringRequest( com.android.volley.Request.Method.GET, url, new Response.Listener<String>() 
            {
                        @Override
                        public void onResponse(String response) 
                        {
                            // Do something with the response
    
                            gson = new Gson();
                            responseobj = gson.fromJson(response, Response.class);
                            customAdapter = new CustomAdapter(getActivity().getApplicationContext(), responseobj.getArticles());
                            recyclerView.setAdapter(customAdapter);
    
                        }
                    },
                    new Response.ErrorListener() 
                    {
                        @Override
                        public void onErrorResponse(VolleyError error) 
                        {
                            // Handle error
                        }
             });
    
            //returning our layout file
            //change R.layout.yourlayoutfilename for each of your fragments
            requestQueue.add(stringRequest);
            return view;
    
        }
    
    
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) 
        {
            super.onViewCreated(view, savedInstanceState);
            //you can set the title for your toolbar here for different fragments different titles
            getActivity().setTitle("GSON Parsing");
        }
    }
    

【问题讨论】:

  • 使用Retrofit库进行json解析是一种更好的做法,效率更高,速度更快
  • 检查Response.Listener的导入
  • 能不能把Response类的代码传过来,我觉得问题出在这里,

标签: android gson android-volley


【解决方案1】:

我认为问题出在您的 Response 类中,根据您的链接应该是这样的:

public class YourResponse {
    @SerializedName("status")
    public String status;
    @SerializedName("source")
    public String source;
    @SerializedName("sortBy")
    public String sortBy;
    @SerializedName("articles")
    public List<Articles> articles;

    public static class Articles {
        @SerializedName("author")
        public String author;
        @SerializedName("title")
        public String title;
        @SerializedName("description")
        public String description;
        @SerializedName("url")
        public String url;
        @SerializedName("urlToImage")
        public String urlToImage;
        @SerializedName("publishedAt")
        public String publishedAt;
    }
}

第二次更改你的片段名称(也许你确实导入错误)

public class FragmentGson extends Fragment 

【讨论】:

    【解决方案2】:

    如果您有 Reponse.listener 无法解析的消息,可能是因为它是未知的。您是否正确添加了像 compile 'com.android.volley:volley:1.0.0' 这样的 Volley 依赖项并在您的 Fragment 中正确导入 Volley ?

    【讨论】:

      【解决方案3】:

      从您粘贴的代码中,我发现Response responseobj 指的是您在片段中创建并导入的响应类。同时,您正在使用 new Response.Listener&lt;&gt;(),它应该是 volley 库中 Response 类的一个实例。现在如果 volley 的 Response 没有被导入,它会显示一个错误。

      这意味着您还必须从 volley 导入 Response 类。确保您已添加这两个导入。

      import com.android.volley.Response;
      

      第二件事是,您必须将片段名称从 Gson 更改为其他名称。否则会出问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        • 2019-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多