public class MyUl extends Thread{
//获取网址传值
String str;
public MyUl(String str) {
this.str = str;
}
public void run() {
super.run();
try {
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
String params = "pageSize=10¤tPage=1";
//获取输出流
OutputStream outputStream = connection.getOutputStream();
outputStream.write(params.getBytes());
//关流
outputStream.flush();
//判断是否链接
if (connection.getResponseCode() == 200){
InputStream inputReader = connection.getInputStream();
StringBuilder builder = new StringBuilder();
byte[] bytes = new byte[1024];
int len = 0;
while ((len=inputReader.read(bytes))!=-1){
builder.append(new String(bytes, 0, len));
}
String json = builder.toString();
//得到值打印到控制台
Log.i("tyx",json);
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}