gpt4 book ai didi

java - 为什么我的 JsonObjectRequest 不工作?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:06:12 24 4
gpt4 key购买 nike

这几天我一直在摆弄这个。做错了。 Android Studio 不会让我用这个错误编译它。所以,我有这个应用程序,其中有两个选项卡和两个 fragment 。一个 fragment 称为 new,该 fragment 获取 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);
}
}

}

Volley 单例

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;
}


}

最佳答案

强制转换 (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) {

}
});

关于java - 为什么我的 JsonObjectRequest 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29247525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com