android - 如何通过 Volley 接收html响应

标签 android web-services android-volley httpresponse

我正在通过 volley 调用网络服务。在该 Web 服务中,我使用 json 对象发送三个输入参数。但作为回应,我收到了 html 响应。 它给了我错误。如何在 onResponse 方法中获取 html 响应?有解决这个问题的方法吗?

  private void callWebServiceToGetDetails(final String uID, final String oID, final String sToken) {
        // initialize the progress dialog and show it
        // Showing progress dialog
        final ProgressDialog progressDialog = new ProgressDialog(HistoryScreenActivity.this, R.style.ProgressBarTansparent);
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.show();
        progressDialog.setContentView(R.layout.custom_progressbar_layout);

        String url = WebServices.GET_DETAILS_WEB_SERVICE;

        JSONObject jsonObjectChield = new JSONObject();
        try {
            jsonObjectChield.put("PUID", uID);
            jsonObjectChield.put("POID", oID);
            jsonObjectChield.put("POPF", "html");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String jsonString = jsonObjectChield.toString();
        Log.d("Params","" + jsonString);

        final JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObjectChield, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                writeToFile(response.toString());

                webViewHistoryDetails.setVisibility(View.VISIBLE);

                File file = new File("" + path);
                webViewHistoryDetails.loadUrl("file:///" + file + "/report.html");
                progressDialog.dismiss();
            }
        }
        // Request a string response from the provided URL.
        , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // Handle your error types accordingly.For Timeout & No connection error, you can show 'retry' button.
                // For AuthFailure, you can re login with user credentials.
                // In this case you can check how client is forming the api and debug accordingly.
                // For ServerError 5xx, you can do retry or handle accordingly.
                if (error instanceof NetworkError) {
                    Toast.makeText(HistoryScreenActivity.this, "No internet connection", Toast.LENGTH_SHORT).show();
                } else if (error instanceof ServerError) {
                    Toast.makeText(HistoryScreenActivity.this, "Our server is busy please try again later", Toast.LENGTH_SHORT).show();
                } else if (error instanceof AuthFailureError) {
                    Toast.makeText(HistoryScreenActivity.this, "AuthFailure Error please try again later", Toast.LENGTH_SHORT).show();
                } else if (error instanceof ParseError) {
                    Toast.makeText(HistoryScreenActivity.this, "Parse Error please try again later", Toast.LENGTH_SHORT).show();
                } else if (error instanceof NoConnectionError) {
                    Toast.makeText(HistoryScreenActivity.this, "No connection", Toast.LENGTH_SHORT).show();
                } else if (error instanceof TimeoutError) {
                    Toast.makeText(HistoryScreenActivity.this, "Server time out please try again later", Toast.LENGTH_SHORT).show();
                }
                error.printStackTrace();
                progressDialog.dismiss();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json");
                // headers.put("Authorization", "Basic c2NvdHQ6dGlnZXI=");
                headers.put("token-id", sToken);
                String creds = String.format("%s:%s", "scott", "tiger");
                String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
                headers.put("Authorization", auth);
                Log.d("SToken",sToken);

                // String creds = String.format("%s:%s", "scott", "tiger");
                // String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
                // headers.put("Authorization", auth);
                return headers;
            }
        };

        Volley.newRequestQueue(HistoryScreenActivity.this).add(postRequest);

    }

我遇到了这个错误

Log cat error

这是我在这个网络服务中得到的响应:

<html>
<head>
<title>Order Details</title>
<style>
body
table td{
    font-family: "Roboto Condensed", sans-serif;
    color: #656565;
    font-size: 14px;
}
</style>
</head>
<body>
<table style="width:100%; cellpadding:0; cellspacing:1;">
    <tr style="background:#CDDDF7;">
        <td style="text-align:center; padding:20px; font-weight:bold; font-size:22px;">Order</td>
    </tr>
    <tr>
        <td>
            <table cellpadding="6" style="width:100%; cellpadding:0; cellspacing:1; background:#F7F7FF; border:1px solid #BEC0CC; border-radius:3px 3px 0px 0px;  padding-bottom:15px;">
            <tbody>
                <tr style="padding-bottom:10px; background-color:#ccc;">
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold;">
                        Order Number 
                    </td>
                    <td style="background-color:F7F7FF; width:30%;">
                        67
                    </td>
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold;">
                        Order Date 
                    </td>
                    <td style="background-color:F7F7FF; width:30%;">
                        2016-07-14
                    </td>
                </tr>
                <tr>
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold;">
                        Customer Name
                    </td>
                    <td style="background-color:F7F7FF; width:30%;">
                        Optim India
                    </td>
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold;">
                        Cook Name
                    </td>
                    <td style="background-color:F7F7FF; width:30%;">
                        Kuldeep Sonawane
                    </td>
                </tr>
                <tr>
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold;">
                        Customer Email
                    </td>
                    <td style="background-color:F7F7FF; width:30%;">
                        ***id@gmail.com
                    </td>
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold;">
                        Cook Email
                    </td>
                    <td style="background-color:F7F7FF; width:30%;">
                        ***id@gmail.com
                    </td>
                </tr>
                <tr>
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold;">
                        Customer Contact Number
                    </td>
                    <td style="background-color:F7F7FF; width:30%; vertical-align:top;">
                        9876******
                    </td>
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold; vertical-align:top;">
                        cook Contact Number
                    </td>
                    <td style="background-color:F7F7FF; width:30%; vertical-align:top;">
                        9658******
                    </td>
                </tr>
                <tr>
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold; vertical-align:top;">
                        order status
                    </td>
                    <td style="background-color:F7F7FF; width:30%;">
                        Accepted
                    </td>
                    <td style="background-color:F7F7FF; width:20%; font-weight:bold; vertical-align:top;">
                        Delivery Address
                    </td>
                    <td style="background-color:F7F7FF; width:30%; vertical-align:top;">
                        clg rd, city - Carlsbad, state - California, country - United States, zip code - 422008
                    </td>
                </tr>
            </tbody>
            </table>
        </td>
    </tr>
    <tr>
        <td>
        <table cellpadding="4" cellspacing="0" style="width:100%; background:#F7F7FF; border:1px solid #BEC0CC; border-top:0!important; border-radius:0 0 3px 3px;">
        <tbody>
            <tr style="font-weight:bold;background-color:#CDDDF7;">
                <td>
                    Sr.
                </td>
                <td >
                    Dish Name
                </td>
                <td style="text-align:right;">
                    Quantity
                </td>
                <td style="text-align:right;">
                    Rate
                </td>
                <td style="text-align:right;">
                    Discount
                </td>
                <td style="text-align:right;">
                    Amount
                </td>
            </tr><tr style="background-color:#fff;">
                    <td style="text-align:center;">
                        1
                    </td>
                    <td>
                        Chicken Tikka Masala
                    </td>
                    <td style="text-align:right;">
                        4
                    </td>
                    <td style="text-align:right;">
                        25000
                    </td>
                    <td style="text-align:right;">
                        0
                    </td>
                    <td style="text-align:right;">
                        100000
                    </td>
                </tr><tr style="background-color:#fff;">
                <td style="border-top:1px solid #ccc;" colspan="4"></td>
                <td style="border-top:1px solid #ccc;">
                    <b>Sub Total</b>
                </td>
                <td style="border-top:1px solid #ccc; text-align:right;">
                    100000
                </td>
            </tr>
            <tr>
                <td style="border-top:1px solid #ccc;" colspan="4"></td>
                <td style="background-color:F7F7FF; font-weight:bold;border-top:1px solid #ccc;">
                    <b>Total</b>
                </td>
                <td style="background-color:F7F7FF; font-weight:bold; border-top:1px solid #ccc; text-align:right;">
                    100000
                </td>
            </tr>
            <tr>
                <td style="border-top:1px solid #ccc;" colspan="4"></td>
                <td style="background-color:F7F7FF; font-weight:bold;border-top:1px solid #ccc;">
                    <b>Total Discount</b>
                </td>
                <td style="background-color:F7F7FF; font-weight:bold; border-top:1px solid #ccc; text-align:right;">
                    0
                </td>
            </tr>
        </tbody>
        </table>
        </td>
    </tr>
</table>
</body>
</html>

最佳答案

我认为您的服务器响应是 html 格式。为了在 java 中解析 html 响应,有一些方法, 但我个人喜欢JSOUP你也可以引用链接thisthis

首先在你的gradle文件中添加依赖

 compile 'org.jsoup:jsoup:1.8.3'  // new version is available i think.. 

然后解析 html 属性,例如。

 Document doc = JSoup.parse(response);
 String id = doc.select("input[name=id]").attr("value");

希望这有帮助..

关于android - 如何通过 Volley 接收html响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38429107/

相关文章:

objective-c - iPhone登录 session 的最佳做法/模式

android - 截击错误未找到身份验证挑战

java - 如何在请求正文中发送嵌套的 json 对象

使用 OpenGL 处理 Android 相机帧

java - 为什么我的 Android 选项卡不能正常工作?

web-services - 是否可以为 sharepoint 2010 开发 WP7 应用程序?

android - 线程式 Web 服务调用

java - Android Volley 请求监听器未触发

android - 使用 Superpowered 在 Android 中进行音高转换/时间拉伸(stretch)

android - SQLite 连接没有直接关系的表