-5

I have written code for POST request from an android application which will run a php file on the server.

    String url = "http://****/****/Servlets.php";

    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
    StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>(){
        @Override
        public void onResponse(String response){
            //code when server responds
        }
    },new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError error){
            //Code if error is there
            int test2 = 1;
        }
    }){
        protected Map<String, String> getParams() {
            Map<String, String> MyData = new HashMap<String, String>();
            MyData.put("key",st);
            return MyData;
        }
    };
    MyRequestQueue.add(MyStringRequest);

Code for the Php file is calling a java file and getting some string value. How do I return that string value to the android device that can be passed to onresponse() parameter? Also does the on response wait or how can we handle it ?

Please suggest.

Ria Sen
  • 94
  • 13
  • 1
    Why double slashes here. `****//****//`? – Jens May 02 '17 at 10:21
  • Is there a way to catch the exception ? It would have been really humble if you would have suggested me a way to post the error – Ria Sen May 02 '17 at 10:21
  • There is a `public void onErrorResponse(VolleyError error){` so print the volleyerror there and see what the response is – Denny May 02 '17 at 10:22
  • public void onErrorResponse(VolleyError error){ log.d(TAG, error); } provide please more information on what you get and what you expect to get – John P May 05 '17 at 07:43
  • There are no errors. I have the code for android application where I have aPOST request to the server. After some processing the server will return a string. How do I handle response on the server side so that I can receive it on the android application side and use it. I see the onReponse(String) function is there but how do we use it ? – Ria Sen May 05 '17 at 07:46
  • 2
    This helps you http://stackoverflow.com/questions/25948191/send-post-request-using-volley-and-receive-in-php – LF00 May 06 '17 at 02:26
  • @Denny This is what I requested. And the below author was considerable enough to answer my question unlike you people who fail to even understand the question. – Ria Sen May 08 '17 at 07:04
  • @warrior Same messgae for the warrior. You failed to understand the question. The below author did. – Ria Sen May 08 '17 at 07:04
  • 1
    Thanks @Kris that link helped me change my approach and come out with a quicker solution. – Ria Sen May 08 '17 at 07:05
  • We fail to understand your question because you did not explain it very well. I'm sure more people agree because you have four downvotes. In the comments you ask how to catch the exception, without any more explanation I assume you mean to catch an exception when the server return an error. Also if you did some research before you know that volley is async and `onResponse` waits for the response to complete. but I'm sure the problem lies in the people who are trying to help – Denny May 08 '17 at 07:34
  • `"How do I return that string value to the android device that can be passed to onresponse() parameter?"` What string value? Pass a parameter to `onResponse()`? The parameter will return something to you, you don't have to pass something to it. – Denny May 08 '17 at 07:38
  • Thanks @Denny for your suggestions. Will work on them and will learn how to ask questions or will never ask again because I can't be taught rather just be downvoted by experienced people like you. – Ria Sen May 08 '17 at 07:46

1 Answers1

1

Try using this code instead of the way you are going :

public String performPostCall(String requestURL, HashMap<String, String> postDataParams) {
    URL url;
    String response = "";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                response += line;
            }
        } else {
            response = "";

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

And then you can get the result by writing the code :

 HashMap<String, String> params = new HashMap<String, String>();
    postDataparams.put("key", value);
 String response = performPostCall("url", postDataParams);

This way you can get the response from your server. On the server side , you just type echo whatever you need to send to the device. Nothing else needs to be done. This Async thread will wait until it gets a response from the server.

Hope this helps.