[英]Error fetching data from server into fragment using volley
I have a json
data from a url in which I want to fetch text data from using volley library. 我有一个来自url的
json
数据,我想在其中使用排球库获取文本数据。 I have also a fragment in which the data would be shown. 我还有一个片段,其中将显示数据。
Volley gives me an error after fetching the data and I have no idea what am doing wrong. Volley在获取数据后给我一个错误,我不知道这是怎么回事。
The onErrorResponse
function gets called again and again and the toast message is shown - Something went wrong. 一次又一次调用
onErrorResponse
函数,并显示Toast消息-出了点问题。
Here's the Fragment.java
这是
Fragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment, container, false);
final TextView textView = view.findViewById(R.id.event_text);
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Config.events_server, (String) null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
textView.setText(response.getString("trending"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(view.getContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
});
MySingleton.getmInstance(view.getContext()).addToRequestQueue(jsonObjectRequest;
return view;
}
And the MySingleton.java
还有
MySingleton.java
public class MySingleton {
private static MySingleton mInstance;
private static Context ctx;
private RequestQueue requestQueue;
private MySingleton(Context ctx) {
this.ctx =ctx;
requestQueue = getRequestQueue();
}
private RequestQueue getRequestQueue() {
if(requestQueue == null) {
requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
}
return requestQueue;
}
public static synchronized MySingleton getmInstance(Context context) {
if(mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public <T>void addToRequestQueue(Request<T> request){
getRequestQueue().add(request);
}
}
Try this: 尝试这个:
Add this in your Singleton classs: 在您的Singleton课程中添加以下内容:
@Override
public void onCreate() {
super.onCreate();
// initialize the singleton
mInstance = this;
}
: :
HashMap<String, String> params = new HashMap<>();
params.put("value","value" );
params.put("value", "value");
JSONObject par = new JSONObject(params);
JsonObjectRequest objectRequest = new JsonObjectRequest("url here", par,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
Log.d(" response", response.toString());
}},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
//handle error here
}
});
// Access the RequestQueue through your singleton class.
MyApplication.getInstance().addToRequestQueue(jsObjRequest);
Let me know in the comments for any further queries 让我知道在评论中是否有其他疑问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.