0

i have tried to use json objectRequest and i was unable to post the data to the php file that i have provide below

    reg=regno.getText().toString().trim();
        psw=password.getText().toString().trim();

        JSONObject postparams=new JSONObject();
        try {
            postparams.put("reg",reg);
            postparams.put("password",psw);
        } catch (JSONException e) {
            e.printStackTrace();
        }

       JsonObjectRequest jor=new JsonObjectRequest(Request.Method.POST, url,postparams , new Response.Listener<JSONObject>() {
           @Override
           public void onResponse(JSONObject response) {
               try {
                   int resultCode=response.getInt("resultCode");
                   switch (resultCode){
                       case 1:
                            Toast.makeText(getApplicationContext(),response.getString("responseDesc"),Toast.LENGTH_SHORT).show();
                            Intent i=new Intent(getApplicationContext(),TabLayout.class);
                            startActivity(i);
                           break;
                       default:
                           Toast.makeText(getApplicationContext(),"Unkown error",Toast.LENGTH_SHORT).show();
                   }
               } catch (JSONException e) {
                   e.printStackTrace();
               }

           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_SHORT).show();

           }
       });




        Mysigleton.getInstance(getApplicationContext()).addRequest(jor);

below is the php code where i want my params to get posted $login=new loginApi($_POST['reg'],$_POST['password']);

cyrus
  • 19
  • 2
  • You can Refer to this post : [StackOverFlow](https://stackoverflow.com/questions/25948191/send-post-request-using-volley-and-receive-in-php/25948689) – Abderazak Amiar Feb 11 '20 at 19:02
  • I was very satisfied with your reference.thank a lot. you have really assisted me a lot – cyrus Feb 11 '20 at 21:18

1 Answers1

0

Try this.. I am using StringRequest in the code bellow but you can use also JsonObjectRequest.

      JSONObject jsonBody = new JSONObject();
        jsonBody.put("reg", reg);
        jsonBody.put("password", psw);

        final String requestBody = jsonBody.toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                Log.i("VolleyResponse", response);

            }

        }, new Response.ErrorListener() {

            public void onErrorResponse(VolleyError error) {

            }

        }) {

            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

            @Override
            public byte[] getBody() {
                return requestBody.getBytes(StandardCharsets.UTF_8);
            }
        };
        AppController.getInstance().addToRequestQueue(stringRequest);
  • welcome in stack ,please add some description with your code to be more readable – Mahmoud Abu Alheja Feb 11 '20 at 22:21
  • after i have posted my parameters ,the expected response is jsonObject type ,will the above code give a string response or jsonObject responce. – cyrus Feb 13 '20 at 21:21
  • when you are using StringRequest the response can be json, xml, text etc... In your case you have to use JSONObject data = new JSONObject(response) and then int resultCode = data.getInt("resultCode") in onResponse – Konstantinos Bakas Feb 13 '20 at 22:04