本帖最后由 田磊阳 于 2013-3-20 10:28 编辑
HTTP协议发送数据:数据小于2K可以使用GET方式。 传送中文参数会出现乱码。
服务端和客户端要统一编码。
主要代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| package com.zyq.service;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class VideoService {
public static boolean save(String name,String time) throws Exception {
Map<string,string> parms=new HashMap<string, string="">();
parms.put("name", name);
parms.put("time", time);
parms.put("method", "save");
String path="http://www.android-study.com/wangluobiancheng/33.html";
return sendGETRequest(path,parms,"UTF-8");
}
/**
* 发送GET请求
* @param path请求路径
* @param parms请求参数
* @return 请求结果
* @throws Exception
*/
private static boolean sendGETRequest(String path, Map<string, string=""> parms,String encoding) throws Exception {
StringBuilder sb=new StringBuilder();
sb.append(path);
if (parms!=null && !parms.isEmpty()) {
sb.append("?");
for (Entry<string, string=""> entry: parms.entrySet()) {
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), encoding))
.append('&');
}
sb.deleteCharAt(sb.length()-1);
}
URL url=new URL(sb.toString());
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setReadTimeout(1000*5);
conn.setRequestMethod("GET");
if (conn.getResponseCode()==200) {
return true;
}
return false;
}
}
</string,></string,></string,></string,string>
|
activity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| package com.zyq.main;
import com.zyq.service.VideoService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText videonameText;
private EditText videotimetext;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name=videonameText.getText().toString().trim();
String time=videotimetext.getText().toString().trim();
try {
boolean bool=VideoService.save(name, time);
if (bool) {
Toast.makeText(MainActivity.this, R.string.succss, 1).show();
}
else {
Toast.makeText(MainActivity.this, R.string.fail, 1).show();
}
}
catch (Exception e) {
Toast.makeText(MainActivity.this, R.string.netfail, 1).show();
e.printStackTrace();
}
}
});
}
}
|
string.xml
1
2
3
4
5
6
7
8
9
| <resources>
<string name="app_name">视频资讯管理器</string>
<string name="viodeoname">视频名称</string>
<string name="viodeotime">视频时长</string>
<string name="button">保存视频参数</string>
<string name="netfail">网络链接失败 !</string>
<string name="succss">保存视频参数成功!</string>
<string name="fail">保存视频参数成功!</string>
</resources>
|
|