【问题标题】:Grabbing Post response from android WebView从 android WebView 获取 Post 响应
【发布时间】:2015-10-29 01:01:12
【问题描述】:

我是使用 Java 进行 Android 编程的新手。我想从网站上获取帖子回复,但我该怎么做?我正在制作一个类似于我的 iOS 应用程序的应用程序。为此在 iOS 中使用 swift 我正在使用这个解决方案 Grabbing POST data from UIWebView

我在网上找不到任何适用于 android 的解决方案 - 请帮助。谢谢

【问题讨论】:

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


    【解决方案1】:

    使用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;
    

    【讨论】:

    • 感谢您的回复@drschultz,但在我的 android 应用程序中,我在接收 JSON 数据之前不会将任何数据发送到服务器,但我在 WebView 中显示的网站会。例如,如果我在我的网站上按下提交按钮。
    • @LocDaiLe - 但是当你在网站上按下提交按钮时,它会发出一个 POST 请求,是吗?因此,您只需切断中间人并直接通过代码发送请求。您是否需要您的用户出于某种原因实际查看网页?还是您真的只关心发出 POST 调用并获得响应?
    • 是的,用户需要查看网页。因为我想在我的网页上使用登录模块,而不是在 android 应用程序上实现它。当用户通过网页(使用电子邮件和密码)登录时,我想获取此帖子数据以在我的应用程序中进一步使用。我想使用这些数据直接从 java 代码发出 post 请求。我希望你明白我的意思。再次感谢。 PS 请参阅我的问题中针对 iOS 中相同问题的链接,该链接中的解决方案可以按照我的意愿工作。
    • @LocDaiLe - 知道了。是的,我理解你想要做什么。您应该在 stackoverflow 上搜索该特定问题。那里有一些答案可以向您展示如何做到这一点。比如这个:http://stackoverflow.com/questions/10213487/how-can-i-get-the-json-response-of-a-post-request-in-a-webview
    • 我已经尝试过了,但我只得到了一些 html 代码,而不是帖子数据:(
    猜你喜欢
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多