为什么我的JsonObjectRequest不起作用?

时间:2015-03-25 04:36:15

标签: java android

我已经习惯了几天了。无法正确行事。 Android工作室不会让我用这个错误编译它。所以,我有这个应用程序,我有两个选项卡和两个片段。一个片段称为new,该片段获取json。但我无法正确地做到这一点。我上传了一张错误图片和类文件的图片。你能帮帮我吗?

错误:"无法解析构造函数JsonObjectRequest(int,java.lang.String,null .......)

The error

new_fragment.java

public class new_fragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private RequestQueue requestQueue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() !=null){
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        volleySingleton = VolleySingleton.getsInstance();
        requestQueue = volleySingleton.getRequestQueue();
        RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue();
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        requestQueue.add(request);
    }

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedIntanceState) {
        setHasOptionsMenu(true);
        View layout = inflater.inflate(R.layout.new_fragment, container, false);
        return layout;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.ref_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle item selection
        switch (item.getItemId()) {
            case R.id.refreshico:
                // do s.th.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

VolleySingleton

public class VolleySingleton {
    private static VolleySingleton sInstance = null;
    private ImageLoader mImageLoader;
    private RequestQueue mRequestQueue;
    private VolleySingleton(){
        mRequestQueue = Volley.newRequestQueue(appClass.getAppContext());
        mImageLoader = new ImageLoader(mRequestQueue,new ImageLoader.ImageCache() {
            private LruCache<String, Bitmap> cache = new LruCache<>((int)(Runtime.getRuntime().maxMemory()/1024)/8);

            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url, bitmap);
            }
        });
    }

    public static VolleySingleton getsInstance(){
        if(sInstance==null){
            sInstance = new VolleySingleton();
        }
        return sInstance;
    }

    public RequestQueue getRequestQueue(){
        return mRequestQueue;
    }
    public ImageLoader getImageLoader(){
        return mImageLoader;
    }


}

6 个答案:

答案 0 :(得分:24)

cast(String)null。

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",(String)null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

答案 1 :(得分:9)

new JsonObjectRequest将第3个参数作为String requestBody

  

将null转换为String

或者您可以创建一个类似String rBody = null;的空字符串,然后将rBody作为第三个参数传递

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
                "http://10.0.8.152/json/new.json", (String) null, // here
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
requestQueue.add(request);

答案 2 :(得分:2)

好吧,我看到了这种情况,并且它发生了,因为正如你的错误所说,构造函数不存在。

如果您使用JsonObjectRequest作为默认值,并且您想要使用Get方法,则不必发送null参数,您应该以这种方式发送:

改变这个:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    System.out.println(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
    requestQueue.add(request);

为此:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,"http://10.0.8.152/json/new.json",
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    System.out.println(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
    requestQueue.add(request);

正如您所看到的,我只是删除了JsonObject的参数,因为该方法是Get,并且有一个构造函数接受您不发送JsonObject。

另一个解决方案是创建自己的JsonObjectRequest,并将其自定义为接受这种值。

问候。

答案 3 :(得分:1)

对于现在正在尝试实现此问题的所有人:Google更改了jsonObjectRequest构造函数:现在您不再提供请求类型,而是先提供url,然后在需要get请求时将null作为第二个参数。 e.g:

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://www.aaa.com/getJSON/dummyMeldung.json", null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("meldung");
                            for (int i = 0; i <= jsonArray.length(); i++) {
                                JSONObject meldung = jsonArray.getJSONObject(i);
                                String comment = meldung.getString("comment");
                                Log.d("ausgabe", comment);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

答案 4 :(得分:0)

有一个含糊不清的构造函数,为了解决这个问题,请使用以下代码:

    JSONObject jsonObject = null;

    JsonObjectRequest request = new JsonObjectRequest(
            Request.Method.GET, "Your url", jsonObject,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                }
            }, 
            new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                }
         });

答案 5 :(得分:0)

与凌空版本有关的问题。 在您的build.gradle应用程序文件中,您应该

dependencies {
......
compile 'com.mcxiaoke.volley:library:1.0.0'
.....
}