1.post请求和get请求比较类似,极少部分不一样
首先还是先自定义一个地址
private String Urlstr = “http://…”;
public void getInfo(String name){
try {
URL url = new URL(Urlstr);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
//标识需要输出参数
urlConnection.setDoOutput(true);
String params = "tel=" + name;
urlConnection.getOutputStream().write(params.getBytes());
urlConnection.getOutputStream().flush();
int responseCode = urlConnection.getResponseCode();
String result;
if(responseCode == 200){
result = Zhuanh(urlConnection.getInputStream(),"gbk");
}else{
result = "error" + responseCode;
}
//更新ui
handler.sendMessage(handler.obtainMessage(UPDATE_UI,result));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}