在新的 WebView 中显示 HttpResponse(来自处理程序的字符串)

dat*_*ers 2 android httpresponse webview android-intent

我在表单的提交按钮 onClickListener 中有以下代码:

String action, user, pwd, user_field, pwd_field;

        action = "theURL";

        user_field = "id";
        pwd_field = "pw";
        user = "username";
        pwd = "password!!";

        List<NameValuePair> myList = new ArrayList<NameValuePair>();
        myList.add(new BasicNameValuePair(user_field, user)); 
        myList.add(new BasicNameValuePair(pwd_field, pwd));

        HttpParams params = new BasicHttpParams();
        HttpClient client = new DefaultHttpClient(params);
        HttpPost post = new HttpPost(action);
        HttpResponse end = null;
        String endResult = null;

        try {
            post.setEntity(new UrlEncodedFormEntity(myList));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        try {
            HttpResponse response = client.execute(post);
            end = response;
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  


        BasicResponseHandler myHandler = new BasicResponseHandler();

        try {
            endResult = myHandler.handleResponse(end);
        } catch (HttpResponseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

如何获取结果字符串 (endResult) 并使用将打开 webview 并加载 html 的意图启动一个新活动?

m6t*_*6tt 5

你可以开始一个新的意图

Intent myWebViewIntent = new Intent(context, MyWebViewActivity.class);
myWebViewIntent.putExtra('htmlString', endResult);
context.startActivity(myWebViewIntent);
Run Code Online (Sandbox Code Playgroud)

然后在你的 MyWebViewActivity 类中,你会有类似的东西:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.my_view_that_contains_a_webview);
    WebView webview = (WebView)findViewById(R.id.my_webview);

    Bundle extras = getIntent().getExtras();
    if(extras != null) {

         // Get endResult
         String htmlString = extras.getString('htmlString', '');
         webview.loadData(htmlString, "text/html", "utf-8");

    }
}
Run Code Online (Sandbox Code Playgroud)