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

标签 android webview android-intent httpresponse

我在表单的提交按钮 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();
        }

如何获取结果字符串 (endResult) 并使用打开 webview 并加载 html 的 Intent 启动新 Activity ?

最佳答案

您可以使用

开始一个新 Intent
Intent myWebViewIntent = new Intent(context, MyWebViewActivity.class);
myWebViewIntent.putExtra('htmlString', endResult);
context.startActivity(myWebViewIntent);

然后在您的 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");

    }
}

关于android - 在新的 WebView 中显示 HttpResponse(来自处理程序的字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3007725/

相关文章:

c# - 如何使用 Xamarin Android 在编辑文本上实现 TextWatcher 接口(interface)?

android - 相对布局权重

java - Android proguard 忽略除一个类之外的所有类

java - 检测 USB 连接并检查它是否是 PC

android - android 属性命名样式的变化有什么意义吗?

Android Webview onKeyDown 强制关闭应用

android - Webview - 在外部浏览器中打开链接?

java - 加载 webview 应用程序时在顶部显示进度条

java - 如何在 Android Studio 中从 java 类意向到 kotlin 类

android - 如何将图像从 Listview 传递到另一个 Activity