简体   繁体   中英

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. 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.

The onErrorResponse function gets called again and again and the toast message is shown - Something went wrong.

Here's the 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

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:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM