Send post data to server using Volley android

后端 未结 5 1518
情话喂你
情话喂你 2021-02-10 04:08

I am trying to send some data to the server using the Volley library.

   private void registerUser(final String email, final String username,
                            


        
5条回答
  •  孤独总比滥情好
    2021-02-10 04:24

     private void postUsingVolley() {
        String tag_json_obj = "json_obj_req";
    
        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("posting...");
        pDialog.show();
    
        final String mVendorId = DeviceDetails.getInstance(mContext).getVendor_id();
        String mUserId = UserModel.getInstance(mContext).getUser_id();
    
        final HashMap postParams = new HashMap();
        sendFeedbackParams.put("key1", value1);
        sendFeedbackParams.put("key2", value2);
        sendFeedbackParams.put("key3", value3);
    
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                ApplicationData.POST_URL, new JSONObject(postParams),
                new com.android.volley.Response.Listener() {
    
                    @Override
                    public void onResponse(JSONObject response) {
                        //Log.d("TAG", response.toString());
                        try {
                            //Toast.makeText(mContext, response.getString("message"), Toast.LENGTH_LONG).show();
                            Toast.makeText(mContext, "Thank you for your post", Toast.LENGTH_LONG).show();
    
                            if (response.getBoolean("status")) {
                                pDialog.dismiss();
                                finish();
                            }
                        } catch (JSONException e) {
                            Log.e("TAG", e.toString());
                        }
                        pDialog.dismiss();
                    }
                }, new com.android.volley.Response.ErrorListener() {
    
            @Override
            public void onErrorResponse(VolleyError error) {
                //VolleyLog.d("TAG", "Error: " + error.getMessage());
                pDialog.dismiss();
                if (isNetworkProblem(error)) {
                    Toast.makeText(mContext, "Internet Problem", Toast.LENGTH_SHORT).show();
    
                }
            }
        }) {
    
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
    
            @Override
            public Map getHeaders() throws AuthFailureError {
                return getRequestHeaders();
            }
        };
    
        jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(8000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
    }
    

    Use Volley like this,... It is working for me.

提交回复
热议问题