0

I am trying to send data using volley.
I have created a app in which it first checks all data and then send data to server using volley method but my app crash as soon as i click on my button to upload the activity can any one help me out if i am send data in correct format or not.

Here is the code which i want to send in form to my server :

{
  "member_Details":[
    {
            "memberID": "1",
            "memberName": "Paddy",
          "mobile": "1",
            "telephone": "07",
            "email": "03",

        },
        {
           "memberID": "1",
            "memberName": "Paddy",
                        "mobile": "1",
            "telephone": "07",
            "email": "03",

        }
    ],
    "FarmerRegID":"130"
}

Code i have tried :

private void uploading_data() {

    String URL = "my url";
    StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("response_login", response);
            try {
                JSONObject json = new JSONObject(response);
                int success = json.getInt("success");
                String msg = json.getString("message");
                if (success == 1) {

                    finish();
                } else {
                    Toast.makeText(Register2Activity.this, msg, Toast.LENGTH_SHORT).show();
                }
            } catch (Exception ex) {
                Log.i("Expection", ex.toString());
            }

        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("volley", "Error: " + error.getMessage());
                    showServerConnectionError();
                }
            }) {

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded; charset=UTF-8";
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();


            params.put("memberID", member_id);
            params.put("memberName", name);
            params.put("mobile", mobile);
            params.put("telephone", telephone);
            params.put("email", email);

            return params;
        }

    };
    RequestQueue queue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();
    queue.add(jsonObjRequest);
}

private void showServerConnectionError() {
    AlertDialog.Builder builder = new AlertDialog.Builder(Register2Activity.this);
    builder.setMessage("Can't establish a connection to server,Please try again!")
            .setTitle("Internet Or Server connection Error !");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    // Create the AlertDialog
    AlertDialog dialog = builder.create();
    dialog.show();
}

1 Answers1

0

Hello @sunny hope you good, I think your question has been answered in stackoverflow before. Please see the link for your reference.

For future reference I recommend you to look on retrofit very cool library that can handle all your network request and works well with the recommended Architectural components and Jetpack i.e RxJava, LiveData, ViewModel. You will just loved it.

Happy Coding!!!