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