0

I'm new to Android programming with Java. I want to grab post response from a website but how can I do that? I'm making an app similar to my iOS app. To this in iOS using swift I'm using this solution Grabbing POST data from UIWebView

I can't find any solution for android on the web - please help. Thank you

Community
  • 1
  • 1
Loc Dai Le
  • 1,661
  • 4
  • 35
  • 70

1 Answers1

0

Getting a POST response is pretty easy using HttpURLConnection. I'm not sure if your project requires that you actually need to use a WebView to view the web page itself, but if you just need to make a POST request and grab the response, I'll post some sample code below to get you started.

In this code, I'm sending in some JSON to a server as a POST request, then retrieving the JSON that the server sends back as a response.

//Http connections and data streams
URL url;
HttpURLConnection httpURLConnection = null;
OutputStreamWriter outputStreamWriter = null;

try {

    //open connection to the server
        url = new URL("your_url_to_web_service");
        httpURLConnection = (HttpURLConnection) url.openConnection();

        //set request properties
        httpURLConnection.setDoOutput(true); //defaults request method to POST
        httpURLConnection.setDoInput(true);  //allow input to this HttpURLConnection
        httpURLConnection.setRequestProperty("Content-Type", "application/json"); //header params
        httpURLConnection.setRequestProperty("Accept", "application/json"); //header params
        httpURLConnection.setFixedLengthStreamingMode(jsonToSend.toString().getBytes().length); //header param "content-length"

        //open output stream and POST our JSON data to server
        outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream());
        outputStreamWriter.write(jsonToSend.toString());
        outputStreamWriter.flush(); //flush the stream when we're finished writing to make sure all bytes get to their destination

        //prepare input buffer and get the http response from server
        StringBuilder stringBuilder = new StringBuilder();
        int responseCode = httpURLConnection.getResponseCode();

        //Check to make sure we got a valid status response from the server,
        //then get the server JSON response if we did.
        if(responseCode == HttpURLConnection.HTTP_OK) {

            //read in each line of the response to the input buffer
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }

            bufferedReader.close(); //close out the input stream

        try {
            //Copy the JSON response to a local JSONArray
            jsonResponse = new JSONArray(stringBuilder.toString());
        } catch (JSONException je) {
            je.printStackTrace();
        }

} catch (IOException ioe) {
    ioe.printStackTrace();
} finally {
    if(httpURLConnection != null) {
        httpURLConnection.disconnect(); //close out our http connection
    }

    if(outputStreamWriter != null) {
        try {
            outputStreamWriter.close(); //close our output stream
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

//Return the JSON response from the server.
return jsonResponse;
NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
  • Thank you for your response @drschultz, but in my android app I don't send any data to a server before receiving the JSON data, but the website that I present in a WebView does. Example if I push a submit button in my website. – Loc Dai Le Oct 28 '15 at 17:57
  • @LocDaiLe - But when you push the submit button in the website, it makes a POST request, yes? So you would just cut out the middleman and send the request directly via code. Do you need your user to actually view the web page for some reason? Or are you really just concerned with making the POST call and getting a response? – NoChinDeluxe Oct 28 '15 at 18:08
  • Yes the user needs to view the webpage. Because I want to use the login module on my webpage instead of implement it on the android app. When a user login via webpage (with email and password) then I want to get this post data for further use in my app. I want to use this data to make post request directly from java code. I hope you understand what i mean . Thanks again. PS see the link in my question for the same issue in iOS the solution in that link work just as i want it to. – Loc Dai Le Oct 28 '15 at 18:14
  • @LocDaiLe - Got it. Yes I understand what you are trying to do. You should do a search on stackoverflow for that specific problem. There are answers out there that show you exactly how to do this. For instance this one: [http://stackoverflow.com/questions/10213487/how-can-i-get-the-json-response-of-a-post-request-in-a-webview](http://stackoverflow.com/questions/10213487/how-can-i-get-the-json-response-of-a-post-request-in-a-webview) – NoChinDeluxe Oct 28 '15 at 19:08
  • I have tried this but I only get some html code back, not the post data :( – Loc Dai Le Oct 29 '15 at 10:02