1
new Thread(){
public void run() {
try {
URL url=new URL("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm");
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
/**
* 超时可以省略
*/
//设置参数
connection.setDoOutput(true); //是否向服务器写参数
StringBuffer sb=new StringBuffer();
sb.append("tel=").append(ed_phone.getText().toString());
byte[] buffer=sb.toString().getBytes();
//写参数,写字节数组(把参数内容转为字节数据)
connection.getOutputStream().write(buffer);
//如果返回ok,处理数据
if(connection.getResponseCode()==200){
InputStream is=connection.getInputStream();//获取字节流;读取服务器返回的数据
byte[] b=new byte[1024];
/**
* 存每次读取的字节
*/
ByteArrayOutputStream arrays=new ByteArrayOutputStream();
int length=0;
while((length=is.read(b))!=-1 ){
arrays.write(b, 0, length);
}
/**
* 再把字节流转为字符串
*/
String con=arrays.toString("gbk");
System.out.println(con);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
2
new Thread() {
@Override
public void run() {
// 将路径转换为URL
try {
URL url = new URL(path);
// 获取连接对象
HttpURLConnection con = (HttpURLConnection) url
.openConnection();
// 设置请求方式
con.setRequestMethod("POST");
// 设置连接超时
con.setConnectTimeout(5000);
// 要发送给服务器的体内容
String data = "username="
+ URLEncoder.encode(e_user, "utf-8") + "&password="
+ URLEncoder.encode(e_pwd, "utf-8");
//设置请求的内容类型 属性 url编码的表单类型
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//设置请求的内容大小 获取内容的长度发送给服务器
con.setRequestProperty("Content-Length", data.length()+"");
//设置打给服务器
con.setDoOutput(true);
//将数据发送给服务器
con.getOutputStream().write(data.getBytes());
//建立连接
con.connect();
//判断响应码
System.out.println("aaaaaa");
if (con.getResponseCode()==200) {
InputStream stream = con.getInputStream();
ByteArrayOutputStream os=new ByteArrayOutputStream();
byte[] b=new byte[1024];
int len=0;
while((len=stream.read(b))!=-1){
os.write(b,0,len);
}
os.close();
final String str = os.toString();
System.out.println(str+"aaaaaaaa");
runOnUiThread(new Runnable() {
@Override
public void run() {
show.setText(str);
}
});
}
else{
runOnUiThread(new Runnable() {
public void run() {
show.setText("失败");
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();