java 为什么我的 JsonObjectRequest 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29247525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my JsonObjectRequest not working?
提问by Richie
I have been fiddling with this for a few days now. Couldn't get it right. Android studio won't let me compile it with this error. So, I have this application where I have two tabs and two fragments. One fragment is called new, and that fragment fetches json. But I couldn't get to do it properly. I have uploaded a picture of how the error looks like, and the class files. Can you please help me out?
几天来我一直在摆弄这个。做对了。Android studio 不允许我用这个错误编译它。所以,我有这个应用程序,其中有两个选项卡和两个片段。一个片段称为新片段,该片段获取 json。但我无法正确地做到这一点。我已经上传了错误的图片和类文件。你能帮帮我吗?
Error: "Cannot resolve constructor JsonObjectRequest(int, java.lang.String, null.......)
错误:“无法解析构造函数 JsonObjectRequest(int, java.lang.String, null.......)
new_fragment.java
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;
}
}
回答by rKrishna
cast (String)null.
强制转换(字符串)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) {
}
});
回答by Kushal Sharma
new JsonObjectRequest
takes 3rd Parameter as String requestBody
new JsonObjectRequest
将第三个参数作为 String requestBody
cast null to String
将 null 转换为 String
or you can make a null string like String rBody = null;
and then pass rBody
as 3rd Parameter
或者您可以创建一个空字符串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);
回答by AlexeyGy
For everyone trying to implement this right now: Google changed the jsonObjectRequest constructor: now you don't suppy the request type anymore, instead you supply firstly the url and then null as second parameter if you want a get request. e.g.:
对于现在尝试实现此功能的每个人:Google 更改了 jsonObjectRequest 构造函数:现在您不再提供请求类型,而是首先提供 url,如果您想要获取请求,则将 null 作为第二个参数。例如:
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) {
}
});
回答by Max Pinto
Well, i see the situation, and it happends because as your error say, the constructor doesnt exists.
好吧,我看到了这种情况,它发生了,因为正如您的错误所说,构造函数不存在。
If you are using the JsonObjectRequest as default, and you want to consume an Get method, you dont have to send the null parameter, you just should send of this way:
如果您使用 JsonObjectRequest 作为默认值,并且您想使用 Get 方法,则不必发送 null 参数,您只需以这种方式发送:
Change this:
改变这个:
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);
For this:
为了这:
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);
As you can see, i just remove the parameter for the JsonObject, because the method is Get, and there is an constructor thats accept that you dont send JsonObject.
如您所见,我只是删除了 JsonObject 的参数,因为该方法是 Get,并且有一个构造函数接受您不发送 JsonObject。
Another solution is to make your own JsonObjectRequest, and custom it to accept that kind of values.
另一种解决方案是制作您自己的 JsonObjectRequest,并自定义它以接受这种值。
Regards.
问候。
回答by javad
There is a ambiguous Constructor, for solve this, use this code:
有一个模棱两可的构造函数,为了解决这个问题,使用以下代码:
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) {
}
});
回答by paulomaranhao
The problem as to do with the version of volley. On your build.gradle app file you should have
与截击版本有关的问题。在您的 build.gradle 应用程序文件中,您应该有
dependencies {
......
compile 'com.mcxiaoke.volley:library:1.0.0'
.....
}