简体   繁体   中英

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

API is sending one HTML page as response. 发布图片

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?

You need to retrieve the content from body

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM