public class RegistActivity extends Activity {
private EditText name, age;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
name = (EditText) findViewById(R.id.editText2name);
age = (EditText) findViewById(R.id.editText1age);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String url="http://172.16.166.195:8080/web/Myservlet";
new HttpThread1(url, name.getText().toString(), age.getText().toString()).start();
}
});
}
1 public class HttpThread1 extends Thread {
2
3 String url, name, age;
4
5 public HttpThread1(String ur, String name, String age) {
6 // TODO Auto-generated constructor stub
7 this.url = ur;
8 this.name = name;
9 this.age = age;
10 }
11
12 private void doGet() {
13 try {
14 url = url + "?name=" +URLEncoder.encode(name, "utf-8") + "&age=" + age;
15 } catch (UnsupportedEncodingException e1) {
16 // TODO Auto-generated catch block
17 e1.printStackTrace();
18 }
19 try {
20 URL httpUrl = new URL(url);
21 try {
22 HttpURLConnection connection = (HttpURLConnection) httpUrl
23 .openConnection();
24
25 connection.setRequestMethod("GET");
26 connection.setReadTimeout(5000);
27
28 BufferedReader reader = new BufferedReader(
29 new InputStreamReader(connection.getInputStream()));
30
31 String str;
32 StringBuffer sBuffer = new StringBuffer();
33 while ((str = reader.readLine()) != null) {
34 sBuffer.append(str);
35 }
36 System.out.println("result=" + sBuffer.toString());
37 } catch (IOException e) {
38 // TODO Auto-generated catch block
39 e.printStackTrace();
40 }
41
42 } catch (MalformedURLException e) {
43 // TODO Auto-generated catch block
44 e.printStackTrace();
45 }
46
47 }
48
49 // ------------------------------------
50
51 private void doPost() {
52 try {
53 URL httpUrl = new URL(url);
54 HttpURLConnection connection = (HttpURLConnection) httpUrl
55 .openConnection();
56 connection.setRequestMethod("POST");
57 connection.setReadTimeout(5000);
58 OutputStream out = connection.getOutputStream();
59 String content = "name=" + name + "&age=" + age;
60 out.write(content.getBytes());
61 BufferedReader reader = new BufferedReader(new InputStreamReader(
62 connection.getInputStream()));
63 StringBuffer sbBuffer = new StringBuffer();
64 String str;
65
66 while ((str = reader.readLine()) != null) {
67
68 sbBuffer.append(str);
69
70 }
71 System.out.println(sbBuffer.toString());
72 } catch (MalformedURLException e) {
73 // TODO Auto-generated catch block
74 e.printStackTrace();
75 } catch (IOException e) {
76 // TODO Auto-generated catch block
77 e.printStackTrace();
78 }
79
80 }
81
82 @Override
83 public void run() {
84 // TODO Auto-generated method stub
85 // doGet();
86 doPost();
87 }
88 }
后台服务器出现中文乱码的原因:浏览器给后台提交数据的时候,Servlet默认使用“iso-8859-1”的编码格式,和服务器的编码格式不同。
解决方法:在 Servlet 中进行转码操作,如图。
new String(name.getBytes("iso-8859-1","utf-8")):先把字符串按照"iso-8859-1"格式转码并转换为字节,再按照"utf-8"格式转码,并转换为字符串。
设置响应头接收的文本类型:response.setContentType("text/html;charset=utf-8"); //文本类型是html,编码格式是utf-8。
客户端使用doGet()方式提交数据,如果含有中文字符,可能就会产生乱码,解决方法:使用 URLEncode.encode(String, "utf-8")进行转码。
举例:url = url + "?name=" + URLEncode.encode(name,"utf-8") + "&age="+age;
而doPost()不用进行转码,因为 Android 手机默认使用的编码格式就是“utf-8”。
那么,如何查看手机使用的编码格式呢?可以先获取手机的系统的属性配置文件(示例代码是写在线程的doPost()方法中):
Properties property = System.getProperties();
打印出配置文件的信息:
property.list( System.out );
运行后,会在后台看到一条信息:file.encoding=UTF-8。
-----------
摘自评论区:
post请求时,只要req.setCharacterEncoding("UTF-8");这样就可以告诉服务器请求以utf-8编码解析,直接就是正确的中文