2

I am building an android app. I have build a request using OKHTTP and I get the response as a string composed of html css and js content. This response is actualy a form that the user must use to allow the app to communicate with a given website.

Now I want the user to be able to see that response as an html page and clicks on a button to allow the communictaion. Only problem I don't know how to display that response as an html in webview or in the web browser.

From the MainActivity:

                          Authenticate myAouth = new Authenticate("myCostumerKey","mySecretKey");
                           try {
                               myResponse=myAouth.run("myUrlHere");
                               //System.out.println( myResponse);
                           } catch (Exception e) {
                               e.printStackTrace();
                           }

the Autheticate class

public class Authenticate {
private final OkHttpClient client;
String[] myResponse =new String[2];
public Authenticate( final String consumerKey, final String consumerSecret) {

    client = new OkHttpClient.Builder()
            .authenticator(new Authenticator() {
                @Override public Request authenticate(Route route, Response response) throws IOException {
                    if (response.request().header("Authorization") != null) {
                        return null; // Give up, we've already attempted to authenticate.
                    }

                    System.out.println("Authenticating for response: " + response);
                    System.out.println("Challenges: " + response.challenges());
                    String credential = Credentials.basic(consumerKey, consumerSecret);
                    Request myRequest =response.request().newBuilder()
                            .header("Authorization", credential)
                            .build();
                    HttpUrl myURL = myRequest.url();
                    myResponse[0]= String.valueOf(myURL);
                    return myRequest;
                }
            })
            .build();
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public String[] run(String url) throws Exception {
    Request request = new Request.Builder()
            .url(url)
            .build();


    try (Response response = client.newCall(request).execute()) {
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        myResponse[1]=response.body().string();
        System.out.println(" URL is "+myResponse[0]+" my response body is "+myResponse[1]);

    }

    return myResponse;
}}

Any help would be apriciated.

Kind Regards

Kamba-Bilola Ted
  • 197
  • 4
  • 12

1 Answers1

4

You can use the following code to convert the String to HTML and then display it in a WebView

    try {
        String html = new String(response, "UTF-8");
        String mime = "text/html";
        String encoding = "utf-8";

        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}
Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
  • Hi Mohamed, thanks for the response. I tried it but it says that String html = new String(response, "UTF-8"); is redudante. is there another way to convert string to UTF-8. more over my webview displays nothing it all blank any help please. – Kamba-Bilola Ted Oct 12 '17 at 07:53
  • This code is working for me when `response` is HTML code. I haven't checked how response looks like in OKHTTP, but you can try `String html = new String(response.body().string(), "UTF-8");` – Mohamed_AbdAllah Oct 12 '17 at 08:41
  • Hi Mohamed yes it actualy works. I realised that webview doesn't work on android emulator I had to use a real device to make it work thanks – Kamba-Bilola Ted Oct 12 '17 at 09:15