Http GET 调用 api 并在 webview 上查看结果

Http GET call to an api and viewing the result on webview

我必须在用户登录时对某个 URL 进行 HTTP GET 调用,并在 WebView[= 中查看 GET 的响应16=]

PlayBack.java(网络视图):

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();
    }
}

但我在视图中没有看到任何内容。我找到了上面的代码here.

试试这个:

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

其中 html == 来自服务器的原始 html 字符串响应。

使用loadDataWithBaseURL代替loadData:

webview.loadDataWithBaseURL("", htmlcon, "text/html", "UTF-8", null);