14.android向web提交参数的4种方式案例

来源:互联网 发布:n个球放入m个盒子C语言 编辑:程序博客网 时间:2024/06/09 15:40

1..get提交的2种方式

第一种方式:

关键代码:

public static String get_save(String name, String phone) {   //传入需要提交的参数,按情况添加多个参数
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path = "http://192.168.0.117/testxml/web.php";
Map<String, String> params = new HashMap<String, String>();   //对参数进行封装
try {
params.put("name", URLEncoder.encode(name, "UTF-8"));  //将中文参数进行编码
params.put("phone", phone);
return sendgetrequest(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "提交失败";
}

 private static String sendgetrequest(String path, Map<String, String> params)throws Exception {
// path="http://192.168.0.117/testxml/web.php?name=xx&phone=xx";
StringBuilder url = new StringBuilder(path);
url.append("?");
for (Map.Entry<String, String> entry : params.entrySet()) {   //将参数与网址组拼
url.append(entry.getKey()).append("=");
url.append(entry.getValue()).append("&");
}
url.deleteCharAt(url.length() - 1);
URL url1 = new URL(url.toString());
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream instream = conn.getInputStream();    //获取页面返回的输入流

//读取返回的数据
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (instream.read(buffer) != -1) {
outstream.write(buffer);
}
instream.close();
return outstream.toString().trim();  //由于返回的数据后面带大量的空格,通过trim()取消空格

}
return "连接失败";
}

第二种方式:

关键代码:

public static String httpclient_getsave(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path = "http://192.168.0.117/testxml/web.php";
Map<String, String> params = new HashMap<String, String>();
try {
params.put("name", name);
params.put("phone", phone);
return sendhttpclient_getrequest(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "提交失败";
}

private static String sendhttpclient_getrequest(String path,Map<String, String> map_params) {
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
for (Map.Entry<String, String> entry : map_params.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String param=URLEncodedUtils.format(params, "UTF-8");
HttpGet getmethod=new HttpGet(path+"?"+param);
HttpClient httpclient=new DefaultHttpClient();
try {
HttpResponse response=httpclient.execute(getmethod);
if(response.getStatusLine().getStatusCode()==200){
return EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}

2.post提交的2种方式:

第一种:关键代码:

public static String httpclient_postsave(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path = "http://192.168.0.117/testxml/web.php";
Map<String, String> params = new HashMap<String, String>();
try {
params.put("name", name);
params.put("phone", phone);
return sendhttpclient_postrequest(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "提交失败";
}

private static String sendhttpclient_postrequest(String path,Map<String, String> params) {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(pairs,"UTF-8");
HttpPost httpost=new HttpPost(path);
httpost.setEntity(entity);
HttpClient client=new DefaultHttpClient();
HttpResponse response= client.execute(httpost);
if(response.getStatusLine().getStatusCode()==200){

return EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}
第二种方式:关键代码

public static String post_save(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path = "http://192.168.0.117/testxml/web.php";
Map<String, String> params = new HashMap<String, String>();
try {
params.put("name", URLEncoder.encode(name, "UTF-8"));
params.put("phone", phone);
return sendpostrequest(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "提交失败";
}

private static String sendpostrequest(String path,
Map<String, String> params) throws Exception {
// name=xx&phone=xx
StringBuilder data = new StringBuilder();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
data.append(entry.getKey()).append("=");
data.append(entry.getValue()).append("&");
}
data.deleteCharAt(data.length() - 1);
}
byte[] entiy = data.toString().getBytes(); // 生成实体数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);// 允许对外输出数据
  conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entiy.length));
OutputStream outstream = conn.getOutputStream();
outstream.write(entiy);
if (conn.getResponseCode() == 200) {
InputStream instream = conn.getInputStream();
ByteArrayOutputStream byteoutstream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (instream.read(buffer) != -1) {
byteoutstream.write(buffer);
}
instream.close();
return byteoutstream.toString().trim();
}
return "提交失败";
}
整个案例的完整代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="姓名:" />

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话:" />

<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="get提交"
android:onClick="get_save"
android:layout_marginRight="30px"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="post提交"
android:onClick="post_save"
android:layout_marginRight="30px"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Httpclient_post提交"
android:onClick="httpclient_postsave"
android:layout_marginRight="30px"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Httpclient_get提交"
android:onClick="httpclient_getsave"/>
<TextView
android:id="@+id/success"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>

////////////////////////////////////////////////////////////////////////////////////////////////////

package caicai.web;

import android.app.Activity;

public class Cai_webActivity extends Activity {

TextView success;
String name;
String phone;
EditText name_view;
EditText phone_view;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name_view=(EditText) findViewById(R.id.name);
phone_view=(EditText) findViewById(R.id.phone);
success=(TextView) findViewById(R.id.success);
}
public void get_save(View v){
name=name_view.getText().toString();
phone=phone_view.getText().toString();
success.setText(service.get_save(name,phone));
}
public void post_save(View v){
name=name_view.getText().toString();
phone=phone_view.getText().toString();
success.setText(service.post_save(name,phone));
}
public void httpclient_postsave(View v){
name=name_view.getText().toString();
phone=phone_view.getText().toString();
success.setText(service.httpclient_postsave(name,phone));
}
public void httpclient_getsave(View v){
name=name_view.getText().toString();
phone=phone_view.getText().toString();
success.setText(service.httpclient_getsave(name,phone));
}



}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package caicai.web;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class service {

public static String get_save(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path = "http://192.168.0.117/testxml/web.php";
Map<String, String> params = new HashMap<String, String>();
try {
params.put("name", URLEncoder.encode(name, "UTF-8"));
params.put("phone", phone);
return sendgetrequest(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "提交失败";
}

private static String sendgetrequest(String path, Map<String, String> params)
throws Exception {

// path="http://192.168.0.117/testxml/web.php?name=xx&phone=xx";
StringBuilder url = new StringBuilder(path);
url.append("?");
for (Map.Entry<String, String> entry : params.entrySet()) {
url.append(entry.getKey()).append("=");
url.append(entry.getValue()).append("&");
}
url.deleteCharAt(url.length() - 1);
URL url1 = new URL(url.toString());
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream instream = conn.getInputStream();
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (instream.read(buffer) != -1) {
outstream.write(buffer);
}
instream.close();
return outstream.toString().trim();

}
return "连接失败";
}

public static String post_save(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path = "http://192.168.0.117/testxml/web.php";
Map<String, String> params = new HashMap<String, String>();
try {
params.put("name", URLEncoder.encode(name, "UTF-8"));
params.put("phone", phone);
return sendpostrequest(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "提交失败";
}

private static String sendpostrequest(String path,
Map<String, String> params) throws Exception {
// name=xx&phone=xx
StringBuilder data = new StringBuilder();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
data.append(entry.getKey()).append("=");
data.append(entry.getValue()).append("&");
}
data.deleteCharAt(data.length() - 1);
}
byte[] entiy = data.toString().getBytes(); // 生成实体数据
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);// 允许对外输出数据
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entiy.length));
OutputStream outstream = conn.getOutputStream();
outstream.write(entiy);
if (conn.getResponseCode() == 200) {
InputStream instream = conn.getInputStream();
ByteArrayOutputStream byteoutstream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (instream.read(buffer) != -1) {
byteoutstream.write(buffer);
}
instream.close();
return byteoutstream.toString().trim();
}
return "提交失败";
}

public static String httpclient_postsave(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path = "http://192.168.0.117/testxml/web.php";
Map<String, String> params = new HashMap<String, String>();
try {
params.put("name", name);
params.put("phone", phone);
return sendhttpclient_postrequest(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "提交失败";
}

private static String sendhttpclient_postrequest(String path,Map<String, String> params) {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(pairs,"UTF-8");
HttpPost httpost=new HttpPost(path);
httpost.setEntity(entity);
HttpClient client=new DefaultHttpClient();
HttpResponse response= client.execute(httpost);
if(response.getStatusLine().getStatusCode()==200){

return EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}


public static String httpclient_getsave(String name, String phone) {
/**
* 出现乱码原因有2个 提交参数没有对中文编码,解决方法:使用URLEncoder.encode(xx,xx)对要提交的汉字转码
* tomatCAT服务器默认采用iso859-1编码,解决方法:把PHP页面保存为UTF-8格式
*/
String path = "http://192.168.0.117/testxml/web.php";
Map<String, String> params = new HashMap<String, String>();
try {
params.put("name", name);
params.put("phone", phone);
return sendhttpclient_getrequest(path, params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "提交失败";
}

private static String sendhttpclient_getrequest(String path,Map<String, String> map_params) {
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
for (Map.Entry<String, String> entry : map_params.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String param=URLEncodedUtils.format(params, "UTF-8");
HttpGet getmethod=new HttpGet(path+"?"+param);
HttpClient httpclient=new DefaultHttpClient();
try {
HttpResponse response=httpclient.execute(getmethod);
if(response.getStatusLine().getStatusCode()==200){
return EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}
///////////////////////////////////////////////////////////////////////////////////////////////////////


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="caicai.web"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".Cai_webActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


原创粉丝点击