将jsonobject发送到服务器

时间:2017-08-09 07:56:55

标签: android json android-volley

我正在尝试将Jsonobject request发送到服务器,我收到volley timeouterror

的回复

我在凌空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);
        }

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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

答案 2 :(得分:0)

你可以尝试

  1. 将volley库添加到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", "era@gmail.com");
          params.put("pass", "123456");
    
    
    
           return params;
       }
    
    };
    
       RequestQueue requestQueue = Volley.newRequestQueue(this);
       requestQueue.add(stringRequest);
      }
    
相关问题