简体   繁体   中英

sending jsonobject to server

i am trying to send the Jsonobject request to the server and i am getting a reply of volley timeouterror

I read something on volley RetryPolicy , that i should increase the time limit of my request and till getting the same error. please guys help to look through my code, and assist me with the right request.

Thanks in advance

  private void getpay() { prgDialog.show(); final String amount = Fund_amount; final String number = Card.getText().toString().trim(); final String year = Year.getText().toString().trim(); final String month = Month.getText().toString().trim(); final String cvv = CVV.getText().toString().trim(); final String pin = Pin.getText().toString().trim(); aaaaaa JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.POST, "https://api.myflex.ng/wallet/fund", new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { prgDialog.cancel(); String data = response.getString("status"); JSONObject obj = response.getJSONObject("data"); JSONObject transfer = obj.getJSONObject("transfer"); Payment_massage = transfer.getString("flutterChargeResponseMessage"); Payment_ref = transfer.getString("flutterChargeReference"); if (data.equalsIgnoreCase("error")) { prgDialog.cancel(); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle); builder.setTitle("myFlex"); builder.setMessage("Connection Error. Please check Your Network Connection"); builder.setCancelable(false); builder.setPositiveButton("OK", null); builder.show(); } else { prgDialog.cancel(); FragmentTransaction trans = getFragmentManager().beginTransaction(); trans.replace(R.id.change_transfer, new card_token()); trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); trans.addToBackStack(null); trans.commit(); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getActivity(), error.toString(),Toast.LENGTH_LONG).show(); prgDialog.cancel(); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle); builder.setTitle("myFlex"); builder.setCancelable(false); builder.setMessage("Connection Error. Please check Your Internet Connection "); builder.setPositiveButton("OK", null); builder.show(); } } ) { @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<String, String>(); headers.put("Authorization", User_Token); headers.put("Content-Type", "application/json"); return headers; } @Override public String getBodyContentType() { return "application/json"; } @Override public byte[] getBody() { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("amount", amount); jsonObject.put("card", come()); } catch (JSONException e) { e.printStackTrace(); } Log.i("Volley", "Error" + String.valueOf(jsonObject)); return jsonObject.toString().getBytes(); } private JSONObject come() throws JSONException { JSONObject params = new JSONObject(); try { params.put("number", number); params.put("expiry_month", month); params.put("expiry_year", year); params.put("cvv", cvv); params.put("pin", pin); Log.i("Volley", "Error" + String.valueOf(params)); } catch (JSONException e) { Log.e("Volley", "Error" + params); } return params; } }; RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); obreq.setRetryPolicy(new DefaultRetryPolicy(100 * 1000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); requestQueue.add(obreq); } 

Try to configure retry policy. Give a large timeout and try

String tag_json_obj = "json_obj_req"; 

obreq.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.addToRequestQueue(obreq, tag_json_obj);

Timeout in Volley is represented in milliseconds. Default time out is 25000 milliseconds or 25 seconds.

int timeOut = 120; // 2 min
int timeOutInMillis = timeOut * 1000;
obreq .setRetryPolicy(new DefaultRetryPolicy(timeOutInMillis,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

requestQueue.add(obreq);

For more details: Transmitting Network Data Using Volley

You can try

  1. add volley library to build.gradle

      compile 'com.mcxiaoke.volley:library:1.0.19' 
  2. You can call like bellow using volley

      private void doLoginAction() { String url_login = "http://10.0.2.2/test_sstem/public/login"; StringRequest stringRequest = new StringRequest(Request.Method.POST, url_login, new Response.Listener<String>() { @Override public void onResponse(String response) { //pDialog.dismiss(); try { JSONObject jsonObject = new JSONObject(response); JSONArray loginNodes = jsonObject.getJSONArray("ParentNode"); for (int i = 0; i < loginNodes.length(); i++) { JSONObject jo = loginNodes.getJSONObject(i); String key1 = jo.getString("key1"); String key2 = jo.getString("key2"); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { try { if (error instanceof TimeoutError ) { //Time out error }else if(error instanceof NoConnectionError){ //net work error } else if (error instanceof AuthFailureError) { //error } else if (error instanceof ServerError) { //Erroor } else if (error instanceof NetworkError) { //Error } else if (error instanceof ParseError) { //Error }else{ //Error } //End } catch (Exception e) { } } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<>(); params.put("uname", "era@gmail.com"); params.put("pass", "123456"); return params; } }; RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); } 

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