2013-08-18 16:16来源:武汉北大青鸟光谷校区作者:武汉宏鹏
关于Android向Web提交参数的4种方式,北大青鸟在此进行总结性分析,如果有兴趣的学员可以当作参考!
种:基于http协议通过get方式提交参数
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 "提交失败"; } get_save()
1.对多个参数的封装
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 "连接失败"; } sendgetrequest
2.提交参数
第二种:基于http协议通过post方式提交参数
1.对多个参数封装
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 "提交失败"; } post_save
2.提交参数
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 "提交失败"; } sendpostrequest()
第三种:基于httpclient类通过post方式提交参数
1.对多个参数的封装
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 "提交失败"; } httpclient_postsave
2.提交参数
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; } sendhttpclient_postrequest()
第四种方式:基于httpclient通过get提交参数
1.多个参数的封装
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 "提交失败"; } httpclient_getsave
2.提交参数
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; } sendhttpclient_getrequest()
4.种方式完整测试案例源码
package caicai.web; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; 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)); } } Cai_webActivity.java
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; } } service.java
<?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> main.xml
<?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> AndroidManifest.xml
Copyright (c) 2006-2021 武汉宏鹏教育咨询有限公司 版权所有 All Rights Reserved.