0

I have to make an HTTP GET call to a certain URL when the user is logged in, and view the response of GET in a WebView

PlayBack.java (WebView):

public class PlayBack extends Activity {
    WebView wv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.play_back);
        wv = new WebView(this);
        wv.setWebViewClient(new CustomWebView());
    }

    private class CustomWebView extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Intent intent = getIntent();
            String id = intent.getStringExtra("pid");
            String playUrl = "certain url"+id;
            String htmlCon = "";
            HttpGet httpGet = new HttpGet(playUrl);
            try{
                HttpResponse httpResponse = MainActivity.httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                InputStream inputStream = httpEntity.getContent();
                htmlCon = convertToString(inputStream);
                wv.loadData(htmlCon, "text/html", "utf-8");
            }catch(Exception e) {

            }
            return false;
        }
    }

    public String convertToString(InputStream inputStream){
        StringBuffer string = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line="";
        try {
            while ((line = reader.readLine()) != null) {
                string.append(line + "\n");
            }
        } catch (Exception e) {}
        return string.toString();
    }
}

But I don't see anything on the view. I found the above code here.

Community
  • 1
  • 1
Aman Grover
  • 1,621
  • 1
  • 21
  • 41

2 Answers2

0

Try this:

webView.loadData(html, "text/html", null);

where html == the raw html String response from the server.

a person
  • 986
  • 6
  • 13
0

Use loadDataWithBaseURL instead of loadData:

webview.loadDataWithBaseURL("", htmlcon, "text/html", "UTF-8", null);
Anton Savin
  • 40,838
  • 8
  • 54
  • 90
Rajiv
  • 1
  • 2