从Android WebView获取Post响应

时间:2015-10-28 15:23:05

标签: java android http-post android-webview response

我是Java新手编程的新手。我想从网站上获取帖子回复,但我该怎么办呢?我正在创建一个类似于我的iOS应用的应用。在使用swift的iOS中,我正在使用此解决方案Grabbing POST data from UIWebView

我在网上找不到任何Android解决方案 - 请帮忙。谢谢

1 个答案:

答案 0 :(得分:0)

使用HttpURLConnection获取POST响应非常简单。我不确定您的项目是否要求您确实需要使用WebView来查看网页本身,但如果您只需要发出POST请求并获取响应,我会发布一些示例代码下面是为了帮助你入门。

在此代码中,我将一些JSON作为POST请求发送到服务器,然后检索服务器作为响应发回的JSON。

//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;