Java 如何将Android活动中的变量提交到网站url?
我构建了一个类似表单的应用程序,它包含四个字段并验证信息,以确保没有输入无效字符。这四个字段存储在变量中:Java 如何将Android活动中的变量提交到网站url?,java,android,http-post,httpurlconnection,Java,Android,Http Post,Httpurlconnection,我构建了一个类似表单的应用程序,它包含四个字段并验证信息,以确保没有输入无效字符。这四个字段存储在变量中: 电话 名字 电子邮件 评论 现在我想将表单数据(输入这四个字段并存储到变量中的内容)提交到url(将使用),但我不确定如何执行此操作。我想我正在寻找一种叫做HttpURLConnection的东西,但我不确定如何指定发送哪个变量。下面的代码是我从网站上找到的 私有类UploadFilesTask扩展了AsyncTask{ 受保护的长doInBackground(URL…URL){ 试一试
- 电话
- 名字
- 电子邮件
- 评论
现在我想将表单数据(输入这四个字段并存储到变量中的内容)提交到url(将使用),但我不确定如何执行此操作。我想我正在寻找一种叫做HttpURLConnection的东西,但我不确定如何指定发送哪个变量。下面的代码是我从网站上找到的
} }私有类UploadFilesTask扩展了AsyncTask{ 受保护的长doInBackground(URL…URL){ 试一试{ HttpClient http=new DefaultHttpClient(); HttpPost=新的HttpPost(“http://www.test.com"); 列表数据=新的ArrayList(); 添加(新的BasicNameValuePair(“电话”、“价值”)); 添加(新的BasicNameValuePair(“名称”、“值”)); 添加(新的BasicNameValuePair(“电子邮件”、“价值”)); 添加(新的BasicNameValuePair(“注释”、“值”)); post.setEntity(新的UrlEncodedFormEntity(数据)); HttpResponse response=http.execute(post); //对回应做点什么 } 捕获(客户端协议例外e){ //做点什么 完成(); } 捕获(IOE异常){ //做点什么 完成(); }
任何帮助都将不胜感激,谢谢 将字段作为参数添加到url
String phone="phone=234432";
String name="name=John Smith";
String email="email=test@email.com";
Url = new URL("http://www.test.com?"+phone+"&"+name+"&"+email);
未测试,但它应该在GET Http请求中工作。将表单数据发送到服务器的最简单方法是使用HttpClient和HttpPost 试着这样做:
try {
HttpClient http = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.example.com/process");
List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("phone", "value");
data.add(new BasicNameValuePair("name", "value");
data.add(new BasicNameValuePair("email", "value");
data.add(new BasicNameValuePair("comments", "value");
post.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = http.execute(post);
// do something with the response
}
catch (ClientProtocolException e) {
// do something
}
catch (IOException e) {
// do something
}
试试看{
HttpClient http=new DefaultHttpClient();
HttpPost=新的HttpPost(“http://www.example.com/process");
列表数据=新的ArrayList();
添加(新的BasicNameValuePair(“电话”、“价值”);
添加(新的BasicNameValuePair(“名称”、“值”);
添加(新的BasicNameValuePair(“电子邮件”、“价值”);
添加(新的BasicNameValuePair(“注释”、“值”);
post.setEntity(新的UrlEncodedFormEntity(数据));
HttpResponse response=http.execute(post);
//对回应做点什么
}
捕获(客户端协议例外e){
//做点什么
}
捕获(IOE异常){
//做点什么
}
注意,您希望在异步任务中执行此操作,这样就不会锁定等待网络操作完成的UI线程
编辑
下面是一个简单的快速示例,说明它在异步任务中可能是什么样子
public class SendTask extends AsyncTask<Void, Void, Boolean> {
String responseString;
@Override
protected Boolean doInBackground(Void... params) {
try {
HttpClient http = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.test.com");
List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("phone", "value"));
data.add(new BasicNameValuePair("name", "value"));
data.add(new BasicNameValuePair("email", "value"));
data.add(new BasicNameValuePair("comments", "value"));
post.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = http.execute(post);
responseString = new BasicResponseHandler().
handleResponse(response); // Basic handler
return true;
}
catch (ClientProtocolException e) {
// do something useful to recover from the exception
// Note: there may not be anything useful to do here
}
catch (IOException e) {
// do something useful to recover from the exception
// Note: there may not be anything useful to do here
}
return false;
}
@Override
protected void onPostExecute(Boolean success) {
// TODO: Do something more useful here
if (success) {
Toast.makeText(MainActivity.this, "Success: " + responseString, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
}
}
公共类SendTask扩展了AsyncTask{
弦乐;
@凌驾
受保护的布尔doInBackground(Void…params){
试一试{
HttpClient http=new DefaultHttpClient();
HttpPost=新的HttpPost(“http://www.test.com");
列表数据=新的ArrayList();
添加(新的BasicNameValuePair(“电话”、“价值”));
添加(新的BasicNameValuePair(“名称”、“值”));
添加(新的BasicNameValuePair(“电子邮件”、“价值”));
添加(新的BasicNameValuePair(“注释”、“值”));
post.setEntity(新的UrlEncodedFormEntity(数据));
HttpResponse response=http.execute(post);
responseString=new BasicResponseHandler()。
HandlerResponse(响应);//基本处理程序
返回true;
}
捕获(客户端协议例外e){
//执行一些有用的操作以从异常中恢复
//注意:此处可能没有任何有用的操作
}
捕获(IOE异常){
//执行一些有用的操作以从异常中恢复
//注意:此处可能没有任何有用的操作
}
返回false;
}
@凌驾
受保护的void onPostExecute(布尔值成功){
//TODO:在这里做一些更有用的事情
如果(成功){
Toast.makeText(MainActivity.this,“Success:+responseString,Toast.LENGTH_LONG).show();
}否则{
Toast.makeText(MainActivity.this,“失败”,Toast.LENGTH_LONG.show();
}
}
}
//使用xml请求
String url="http://www.test.com";
String xmlRequest="<message xmlns=\"http://test.com/schemas/ma\"><header></header>"+
"<body><phone>9944556622</phone><name>Mytest name</name><email>mytest@email.com</email>"+"<comments>hay this seems nice</comments></body></message>"
HttpPost httppost = new HttpPost(url);
StringEntity se = new StringEntity(xmlRequest,HTTP.UTF_8);
httppost.setHeader("Content-Type","application/xml");
httppost.setHeader("Accept","application/xml");
httppost.setEntity(se);
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);
StatusLine statusLine = httpResponse.getStatusLine();
stringurl=”http://www.test.com";
字符串xmlRequest=“”+
“994456622MyTestnamemytest@email.com“+“这看起来不错”
HttpPost HttpPost=新的HttpPost(url);
StringEntity se=新的StringEntity(xmlRequest,HTTP.UTF_8);
setHeader(“内容类型”、“应用程序/xml”);
setHeader(“接受”、“应用程序/xml”);
httppost.setEntity(se);
BasicHttpResponse httpResponse=(BasicHttpResponse)httpclient.execute(httppost);
StatusLine StatusLine=httpResponse.getStatusLine();
HttpClient-HttpClient=newdefaulthttpclient();
Log.i(“sampath”,“第139行”);
HttpPost HttpPost=新的HttpPost(url);
BasicNameValuePair用户名BasicNameValuePair=新的BasicNameValuePair(“textMessage”,msg);
List nameValuePairList=新的ArrayList();
nameValuePairList.add(usernameBasicNameValuePair);
试一试{
UrlEncodedFormEntity UrlEncodedFormEntity=新的UrlEncodedFormEntity(nameValuePairList);
setEntity(urlEncodedFormEntity);
试一试{
HttpResponse HttpResponse=httpClient.execute(httpPost);
InputStream InputStream=httpResponse.getEntity().getContent();
InputStreamReader InputStreamReader=新的InputStreamReader(inputStream);
BufferedReader BufferedReader=新的BufferedReader(inputStreamReader);
StringBuilder StringBuilder=新的StringBuilder();
String url="http://www.test.com";
String xmlRequest="<message xmlns=\"http://test.com/schemas/ma\"><header></header>"+
"<body><phone>9944556622</phone><name>Mytest name</name><email>mytest@email.com</email>"+"<comments>hay this seems nice</comments></body></message>"
HttpPost httppost = new HttpPost(url);
StringEntity se = new StringEntity(xmlRequest,HTTP.UTF_8);
httppost.setHeader("Content-Type","application/xml");
httppost.setHeader("Accept","application/xml");
httppost.setEntity(se);
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);
StatusLine statusLine = httpResponse.getStatusLine();
HttpClient httpClient = new DefaultHttpClient();
Log.i("sampath","line 139");
HttpPost httpPost = new HttpPost(url);
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("textMessage", msg);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
try {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
httpPost.setEntity(urlEncodedFormEntity);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
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("Firstption caz of HttpResponese :" + cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Secondption caz of HttpResponse :" + ioe);
ioe.printStackTrace();
}
} catch (UnsupportedEncodingException uee) {
System.out.println("Anption given because of UrlEncodedFormEntity argument :" + uee);
uee.printStackTrace();
}
//---------------------------------------------------------------------------
Here is the php file
<?php
$message = $_POST['textMessage'];
echo 'working fine'.$message;
$filename="androidmessages.html";
file_put_contents($filename,"Entered Text is:".$message."<br />",FILE_APPEND);
?>