android客户端通过Get方式提交参数给服务器.docx
- 文档编号:9312740
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:18
- 大小:49.72KB
android客户端通过Get方式提交参数给服务器.docx
《android客户端通过Get方式提交参数给服务器.docx》由会员分享,可在线阅读,更多相关《android客户端通过Get方式提交参数给服务器.docx(18页珍藏版)》请在冰豆网上搜索。
android客户端通过Get方式提交参数给服务器
android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
转帖,原帖地址
为学习存档~~~
服务器端的设置
服务器端采用Struts2来接收android端的请求,android版本为2.2.3配置如下:
1:
web.xml的配置为
[html]viewplaincopyprint?
1.
xml version="1.0" encoding="UTF-8"?
>
2. xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation=" 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.
2:
VideoManageAction类
[java]viewplaincopyprint?
1.package com.capinfotech.android;
2.
3.import javax.servlet.http.HttpServletRequest;
4.import javax.servlet.http.HttpServletResponse;
5.
6.import org.apache.struts2.interceptor.ServletRequestAware;
7.import org.apache.struts2.interceptor.ServletResponseAware;
8.
9.import com.opensymphony.xwork2.ActionSupport;
10.
11.public class VideoManageAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
12.
13. private HttpServletRequest request;
14. private HttpServletResponse response;
15.
16. public void setServletRequest(HttpServletRequest request) {
17. this.request = request;
18. }
19.
20. public void setServletResponse(HttpServletResponse response) {
21. this.response = response;
22. }
23.
24. public void save() {
25. String method = this.request.getMethod(); //获得requst的方法
26. String title = null;
27. Integer timelength = null;
28. if("GET".equals(method)) {
29. title = request.getParameter("title"); //获得参数title的值
30. timelength = new Integer(request.getParameter("timelength")); //获得参数timelength的值
31. System.out.println("title:
" + title); //输出title的值
32. System.out.println("timelength:
" + timelength.intValue()); //输出timelength的值
33.
34. } else {
35. title = request.getParameter("title");
36. timelength = new Integer(request.getParameter("timelength"));
37. System.out.println("title:
" + title);
38. System.out.println("timelength:
" + timelength.intValue());
39. }
40. }
41.}
3:
struts.xml的配置为
[html]viewplaincopyprint?
1.
xml version="1.0" encoding="UTF-8" ?
>
2.
DOCTYPE struts PUBLIC
3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4. "http:
//struts.apache.org/dtds/struts-2.0.dtd">
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
android端的配置
1:
androidmanifest.xml的内容
[html]viewplaincopyprint?
1.
xml version="1.0" encoding="utf-8"?
>
2. android=" 3. package="com.capinfotech.upload" 4. android: versionCode="1" 5. android: versionName="1.0"> 6. icon="@drawable/icon" android: label="@string/app_name"> 7. name="android.test.runner" /> 8. name=".MainActivity" 9. android: label="@string/app_name"> 10. 11. name="android.intent.action.MAIN" /> 12. name="android.intent.category.LAUNCHER" /> 13. 14. 15. 16. 17. minSdkVersion="8" /> 18. name="android.permission.INTERNET" /> 19. name="android.test.InstrumentationTestRunner" 20. android: targetPackage="com.capinfotech.upload" android: label="Tests for My App" /> 21.
2:
HttpRequest类的内容
[html]viewplaincopyprint?
1.package ;
2.
3.import java.io.IOException;
4.import .HttpURLConnection;
5.import .MalformedURLException;
6.import .URL;
7.import java.util.Map;
8.
9.import android.util.Log;
10.
11.public class HttpRequest {
12. private static final String TAG = "HttpRequest";
13. public static boolean sendGetRequest(String path, Map
14. /*
15. * http:
//127.0.0.1/AndroidService/android/upload?
title=aaa&timelength=90的形式
16. */
17. StringBuilder sb = new StringBuilder(path);
18. sb.append('?
');
19. for(Map.Entry
params.entrySet()) {
20. sb.append(entry.getKey()).append('=').append(entry.getValue()).append('&');
21. }
22. sb.deleteCharAt(sb.length()-1);
23.
24. try {
25. URL url = new URL(sb.toString());
26. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
27. conn.setRequestMethod("GET"); //设置方法为GET
28. conn.setReadTimeout(5 * 1000); //设置过期时间为5秒
29. if(conn.getResponseCode() == 200) { //如果成功返回
30. return true;
31. }
32. } catch (MalformedURLException e) {
33. e.printStackTrace();
34.
35. Log.e(TAG, e.toString());
36. return false;
37. }
38. return false;
39. }
40.}
3:
HttpRequestTest类
[java]viewplaincopyprint?
1.package com.capinfotech.test;
2.
3.import java.io.IOException;
4.import java.util.HashMap;
5.import java.util.Map;
6.
7.import android.test.AndroidTestCase;
8.import android.util.Log;
9.
10.import .HttpRequest;
11.
12.public class HttpRequestTest extends AndroidTestCase {
13. private static final String TAG = "HttpRequestTest";
14.
15. public void testSendGetRequest() throws IOException {
16. Map
17. params.put("title", "long");
18. params.put("timelength", "80");
19.
20. String path = "http:
//192.168.1.105/AndroidService/android/upload";
21. if(HttpRequest.sendGetRequest(path, params)) {
22. Log.i(TAG, "success");
23. } else {
24. Log.i(TAG, "failure");
25. }
26. }
27.}
4:
测试结果页面
上传的参数中含有中文时
改变方法testSendGetRequest()方法里的几行程序:
[java]viewplaincopyprint?
1.Map
2. params.put("title", "变形金刚3");
3. params.put("timelength", "80");
测试结果页面为:
需要修改的程序:
1:
HttpRequest类修改为:
[java]viewplaincopyprint?
1.package ;
2.
3.import java.io.IOException;
4.import .HttpURLConnection;
5.import .MalformedURLException;
6.import .URL;
7.import .URLEncoder;
8.import java.util.Map;
9.
10.import android.util.Log;
11.
12.public class HttpRequest {
13. private static final String TAG = "HttpRequest";
14. public static boolean sendGetRequest(String path, Map
15.
16. /*
17. * http:
//127.0.0.1/AndroidService/android/upload?
title=aaa&timelength=90的形式
18. */
19. StringBuilder sb = new StringBuilder(path);
20. sb.append('?
');
21. for(Map.Entry
params.entrySet()) {
22. sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');
23. }
24. sb.deleteCharAt(sb.length()-1);
25.
26. try {
27. URL url = new URL(sb.toString());
28. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
29. conn.setRequestMethod("GET"); //设置方法为GET
30. conn.setReadTimeout(5 * 1000); //设置过期时间为5秒
31. if(conn.getResponseCode() == 200) { //如果成功返回
32. return true;
33. }
34. } catch (MalformedURLException e) {
35. e.printStackTrace();
36.
37. Log.e(TAG, e.toString());
38. return false;
39. }
40. return false;
41. }
42.}
主要是在函数中添加了一个表示编码的参数enc,用来控制编码的格式问题
2:
HttpRequest类修改为
[java]viewplaincopyprint?
1.public class HttpRequestTest extends AndroidTestCase {
2. private static final String TAG = "HttpRequestTest";
3.
4. public void testSendGetRequest() throws IOException {
5. Map
6. params.put("title", "变形金刚3");
7. params.put("timelength", "80");
8.
9. String path = "http:
/
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- android 客户端 通过 Get 方式 提交 参数 服务器
![提示](https://static.bdocx.com/images/bang_tan.gif)