如何使用 Android 中的改造接收 HTML 页面作为来自 API 的响应?

发布于 2025-01-13 22:35:00 字数 1877 浏览 3 评论 0原文

API 正在发送一个 HTML 页面作为响应。 帖子图片

目前我正在接收 ResponseBody,

public interface DashBoardHtmlJsonAPI {
    @FormUrlEncoded
    @POST("/MobileDashbord")
    Call<ResponseBody> getHtml(@Field("UserName") String username, @Field("Password") String password);
}

我想将 responseBody 转换为 String 并将 html 加载到 webview。

html  = response.body().string();

成功响应后,上述代码返回一个空白字符串,

我无法将 html 页面作为字符串检索。

Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.BASE_URL).addConverterFactory(ScalarsConverterFactory.create()).build();
        DashBoardHtmlJsonAPI dashBoardHtmlJsonAPI = retrofit.create(DashBoardHtmlJsonAPI.class);
        Call<ResponseBody> call = dashBoardHtmlJsonAPI.getHtml(userName,password);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()){
                    String html="";
                    try {
                      html  = response.body().string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    loadWebView(html);
                    Log.i("WEBVIEW",response.toString());
                    Log.i("WEBVIEW",html);


                }
                else {
                    Log.i("WEBVIEW",response.body().toString());

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i("WEBVIEW",t.getLocalizedMessage());
            }
        });

有人可以帮助我吗?

API is sending one HTML page as response.
postment image

Currently i am receiving as ResponseBody

public interface DashBoardHtmlJsonAPI {
    @FormUrlEncoded
    @POST("/MobileDashbord")
    Call<ResponseBody> getHtml(@Field("UserName") String username, @Field("Password") String password);
}

I want convert the responseBody to String and load the html to webview.

html  = response.body().string();

After successful response above code return a blank string

I am not able to retrieve the html page as string.

Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.BASE_URL).addConverterFactory(ScalarsConverterFactory.create()).build();
        DashBoardHtmlJsonAPI dashBoardHtmlJsonAPI = retrofit.create(DashBoardHtmlJsonAPI.class);
        Call<ResponseBody> call = dashBoardHtmlJsonAPI.getHtml(userName,password);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()){
                    String html="";
                    try {
                      html  = response.body().string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    loadWebView(html);
                    Log.i("WEBVIEW",response.toString());
                    Log.i("WEBVIEW",html);


                }
                else {
                    Log.i("WEBVIEW",response.body().toString());

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i("WEBVIEW",t.getLocalizedMessage());
            }
        });

Can anybody help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天 2025-01-20 22:35:00

您需要从正文中检索内容

try {
   html  = response.body().content();
} catch (IOException e) {
   e.printStackTrace();
}

You need to retrieve the content from body

try {
   html  = response.body().content();
} catch (IOException e) {
   e.printStackTrace();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文