简体   繁体   English

我如何使用 Android 中的 retrofit 从 API 接收 HTML 页面作为响应?

[英]How do i receive HTML page as response from API using retrofit in Android?

API is sending one HTML page as response. API 正在发送一个 HTML 页面作为响应。 发布图片

Currently i am receiving as ResponseBody目前我正在接收 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.我想将 responseBody 转换为 String 并将 html 加载到 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.我无法将 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());
            }
        });

Can anybody help me?有谁能够帮我?

You need to retrieve the content from body您需要从正文中检索内容

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android:如何使用 Retrofit 接收来自以下 json 的响应? - android: How to receive response from the following json using Retrofit? 我无法在使用 android 中使用 java 的改造 2 中收到呼叫响应无法解析 model - I cannot receive response in call using retrofit2 using java in android cannot parse response in model Null 指针异常来自 api 响应在 android 工作室中使用 retrofit - Null pointer exception from api response using retrofit in android studio 使用 Retrofit 和 PHP 作为后端,无法从 android 中的服务器接收任何响应 - Failed to receive any response from server in android using Retrofit and PHP as backend 如何使用Retrofit和RxJava / RxAndroid处理响应错误? - How do I handle response errors using with Retrofit and RxJava/RxAndroid? Android:如何使用改型从json响应中获取特定字符串? - Android: How to get specific string from json response using retrofit? How to get api request and api response time in Android Retrofit api using OkHttp Interceptor for each api? - How to get api request and api response time in Android Retrofit api using OkHttp Interceptor for each api? 使用改造从API返回信息,如何在其他类中使用它? - Using retrofit to return information from an API, how do I use this in other classes? 如何从改造Android获得错误响应 - How to get error response from retrofit android 如何使用 Android 中的 Retrofit 从 API 获取数据 - How to Fetch data from API using Retrofit in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM