Send post data to server using Volley android

后端 未结 5 1516
情话喂你
情话喂你 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-10 04:50

    This is the solution. I had to use the JsonObjectRequest class and not the StringRequest on. What JsonRequest does is that it converts your HashMap key-value pairs into a JSON Format.

        private void registerUser(String email_address,String username, String 
        password) {
        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();
        String location = getResources().getConfiguration().locale.getCountry();
        final HashMap postParams = new HashMap  
        ();
        postParams.put("username", username);
        postParams.put("email", email_address);
        postParams.put("password", password);
        postParams.put("location", location);
    
    
    
        Response.Listener  listener;
        Response.ErrorListener errorListener;
        final JSONObject jsonObject = new JSONObject(postParams);
    
        JsonObjectRequest jsonObjReq = new 
        JsonObjectRequest(AppConfig.URL_REGISTER, jsonObject,
                new com.android.volley.Response.Listener() {
    
                    @Override
                    public void onResponse(JSONObject response) {
                        //Log.d("TAG", response.toString());
                        try {
                            Toast.makeText(getApplicationContext(), 
           response.getString("message"), Toast.LENGTH_LONG).show();
                            // Toast.makeText(getApplicationContext(), "Thank 
           you for your post", Toast.LENGTH_LONG).show();
    
                            if (response.getString("status").equals("success")){
    
    
                                session.setLogin(true);
    
    
                                pDialog.dismiss();
                               Intent i = new 
            Intent(RegisterActivity.this,Welcome.class);
                               startActivity(i);
    
                                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();
    
            }
        }) {
    
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
    
    
        };
    
    
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
    
    
    
      }
    

提交回复
热议问题