使用 android 应用程序的输入数据自动填充网站表单
Posted
技术标签:
【中文标题】使用 android 应用程序的输入数据自动填充网站表单【英文标题】:Autofill a form of a website with input data of an android app 【发布时间】:2015-09-18 15:34:58 【问题描述】:我是 android 开发人员的新手,我在互联网上进行了很多搜索,我找到了一种将数据发送到网站的解决方案,但由于大多数命令已弃用,我不知道该怎么做。 我想通过与我的 android 应用程序交互来填写网站表单(如登录表单)并向其发送数据。 (使用 url 连接)。 这是已弃用代码的某些部分: HttpClient httpClient = new DefaultHttpClient();
// In a POST request, we don't pass the values in the URL.
//Therefore we use only the web page URL as the parameter of the HttpPost argument
HttpPost httpPost = new HttpPost("http://my2film7.in/");
// Because we are not passing values over the URL, we should have a mechanism to pass the values that can be
//uniquely separate by the other end.
//To achieve that we use BasicNameValuePair
//Things we need to pass with the POST request
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("paramUsername", paramUsername);
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("paramPassword", paramPassword);
// We add the content that we want to pass with the POST request to as name-value pairs
//Now we put those sending details to an ArrayList with type safe of NameValuePair
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePAir);
try
// UrlEncodedFormEntity is an entity composed of a list of url-encoded pairs.
//This is typically useful while sending an HTTP POST request.
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
// setEntity() hands the entity (here it is urlEncodedFormEntity) to the request.
httpPost.setEntity(urlEncodedFormEntity);
try
// HttpResponse is an interface just like HttpPost.
//Therefore we can't initialize them
HttpResponse httpResponse = httpClient.execute(httpPost);
// According to the JAVA API, InputStream constructor do nothing.
//So we can't initialize InputStream although it is not an interface
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null)
stringBuilder.append(bufferedStrChunk);
return stringBuilder.toString();
catch (ClientProtocolException cpe)
System.out.println("First Exception caz of HttpResponese :" + cpe);
cpe.printStackTrace();
catch (IOException ioe)
System.out.println("Second Exception caz of HttpResponse :" + ioe);
ioe.printStackTrace();
catch (UnsupportedEncodingException uee)
System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
uee.printStackTrace();
return null;
【问题讨论】:
【参考方案1】:将您的代码更新为以下内容:
// Get user defined values
Name = "your username here";
Email = "email";
Login = "user";
Pass = "password";
// Create data variable for sent values to server
String data = URLEncoder.encode("name", "UTF-8")
+ "=" + URLEncoder.encode(Name, "UTF-8");
data += "&" + URLEncoder.encode("email", "UTF-8") + "="
+ URLEncoder.encode(Email, "UTF-8");
data += "&" + URLEncoder.encode("user", "UTF-8")
+ "=" + URLEncoder.encode(Login, "UTF-8");
data += "&" + URLEncoder.encode("pass", "UTF-8")
+ "=" + URLEncoder.encode(Pass, "UTF-8");
String text = "";
BufferedReader reader=null;
// Send data
try
// Defined URL where to send data
URL url = new URL("http://androidexample.com/media/webservice/httppost.php");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
// Append server response in string
sb.append(line + "\n");
text = sb.toString();
catch(Exception ex)
finally
try
reader.close();
catch(Exception ex)
// Show response on activity
content.setText( text );
您需要使用 URLConnection
并使用流写入值,而不是已弃用的 httpPost
和 namevaluepairs
。
【讨论】:
这一行有一个异常:OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());你能告诉我这是什么原因造成的吗? 您的连接可能没有建立。您可以检查conn
是否为空。以上是关于使用 android 应用程序的输入数据自动填充网站表单的主要内容,如果未能解决你的问题,请参考以下文章