本文原理:将数据拼接在访问的服务器地址后面,形式如下
http://192.168.1.100:8080/web_login/login?number=123&pwd=123
其中number和pwd是要上传的数据,然后设置请求方式为GET,访问服务器后拿到服务器返回结果。最后handler更新UI
new Thread(){
public void run() {
try {
path = path+"number="+URLEncoder.encode(number, "UTF-8")+"&pwd="+URLEncoder.encode(pwd, "UTF-8");
System.out.println(path);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求方式为get
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if(code==200){
InputStream in = conn.getInputStream();
String data = StreamTool.decodeStream(in);
Message msg = Message.obtain();
msg.what=SUCCESS;
msg.obj = data;
mHandler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what=ERROR;
mHandler.sendMessage(msg);
}
};
}.start();
服务器端解决以get方式提交的乱码:方式如下:
原理:用什么方式编码就用什么方式解码,这样才不会乱码