问题描述
我一直在网上搜索的方式,现在做到这一点的一个星期左右,我只是似乎无法找到答案。
I've been searching the web for a way to do this for about a week now, and I just can't seem to figure it out.
我想实现一个应用程序,我的大学可以用它来允许用户登录到不同的服务与轻松的校园。它的工作原理目前的方式是他们去一个在线门户网站,选择他们想要的,填写自己的用户名和pwd哪些服务,然后单击登录。通过邮寄发送的表格数据(它包括几个隐藏的价值观以及刚刚用户名和PWD)到相应的登录脚本,然后签署他们并加载该服务。
I'm trying to implement an app that my college can use to allow users to log in to various services on the campus with ease. The way it works currently is they go to an online portal, select which service they want, fill in their user name and pwd, and click login. The form data is sent via post (it includes several hidden values as well as just the user name and pwd) to the corresponding login script which then signs them in and loads the service.
我一直在苦思这个问题有两种方式。我第一次尝试一个web视图,但它似乎并不希望支持所有HTML通常使这种形式的工作。我得到我需要的所有元素,字段的用户和pwd以及登录按钮,但点击按钮不会做任何事情。我想,如果我需要添加一个onclick处理程序,但我看不出作为按钮在web视图的HTML未使用独立的Android元素来实现。
I've been trying to come at the problem in two ways. I first tried a WebView, but it doesn't seem to want to support all of the html that normally makes this form work. I get all of the elements I need, fields for user and pwd as well as a login button, but clicking the button doesn't do anything. I wondered if I needed to add an onclick handler for it, but I can't see how as the button is implemented in the html of the webview not using a separate android element.
另一种可能是使用XML小部件来创建一个漂亮的相对布局,这似乎加载速度更快,并期待在Android屏幕上更好的形式。我用的EditText字段输入,一个微调部件的服务选择,和按钮控件的登录。我知道该怎么做的onclick和项目选择处理程序按钮,微调,分别为,但我无法弄清楚如何通过POST的意图,将随后启动浏览器发送数据。我可以做的动作URL的意图,但无法获得POST数据喂进去。
The other possibility was using the xml widgets to create the form in a nice relative layout, which seems to load faster and looks better on the android screen. I used EditText fields for the input, a spinner widget for the service select, and the button widget for the login. I know how to make the onclick and item select handlers for the button and spinner, respectively, but I can't figure out how to send that data via POST in an intent that would then launch a browser. I can do an intent with the action url, but can't get the POST data to feed into it.
因此,这里是我现在...
So here is what I have right now...
HttpParams params = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost(action);
String endResult = null;
try
{
post.setEntity(new UrlEncodedFormEntity(myList));
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
String response = client.execute(post, new BasicResponseHandler());
endResult = response;
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
所以,我现在的问题......是我怎么走endResult屏幕,应返回的页面后,我登录到我的服务,并在浏览器中显示它?
So my question now... is how do I take the endResult screen, which should be the page returned after I logged in to my service, and display it in a browser?
推荐答案
有什么不对他们只使用内置的浏览器?您也可以通过提交一个表单<一个href="http://developer.android.com/reference/org/apache/http/client/entity/UrlEn$c$cdFormEntity.html"相对=nofollow> UrlEn codedFormEntity 并HttpClient的。
What's wrong with them just using the built in browser? You can also submit a form using UrlEncodedFormEntity and HttpClient.
HttpParams params = new DefaultHttpParams(); // setup whatever params you what
HttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost("someurl");
post.setEntity(new UrlEncodedFormEntity()); // with list of key-value pairs
client.execute(post, new ResponseHandler(){}); // implement ResponseHandler to handle response correctly.
好了以后,你有一个字符串的响应。因为它的页面的响应将是在HTML。你需要使用的WebView显示HTML。的WebView有一个方法loadData(),它的HTML的字符串并显示。
Okay and after you have the response in a string. The response since its a page is going to be in html. You need to use a WebView to show the html. WebView has a method loadData() that takes a string of html and displays it.
这篇关于在Android应用POST数据提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!