2017-08-09 87 views
0

我试图将Jsonobject request发送到服务器和我收到的volley timeouterror发送的JSONObject服务器

我读凌空RetryPolicy东西的答复,我应该增加我的请求的时间限制,直到获得同样的错误。请大家帮忙查看我的代码,并协助我提供正确的请求。

在此先感谢

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

+0

为什么不使用凌空 –

+0

我可以使用,请给我选择,告诉我该怎么办呢 –

+0

你在邮递员试过吗? – Jeevanandhan

回答

0

尝试配置重试策略。给一个很大的超时时间并尝试

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); 
0

超时在Volley以毫秒为单位表示。默认超时是25000毫秒或25秒。

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

有关详细信息:Transmitting Network Data Using Volley

0

您可以尝试

  1. 附加凌空库的build.gradle

    compile 'com.mcxiaoke.volley:library:1.0.19' 
    
  2. 可以调用像波纹管采用凌空

    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", "[email protected]"); 
         params.put("pass", "123456"); 
    
    
    
         return params; 
        } 
    
    }; 
    
        RequestQueue requestQueue = Volley.newRequestQueue(this); 
        requestQueue.add(stringRequest); 
        } 
    
+0

您可以轻松地提交您的请求并迅速得到回复 –