httpurlconnection
get方式请求
URL url=new URL(params[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
if(connection.getResponseCode()==200){
InputStream is=connection.getInputstream();
String json=jiesonutils.getjson(is);
return
}
post
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(et.getText().toString())
//写参数,写字节数组(吧参数内容转为字节数组)
byte[] be=sb.toString().getBytes()
connection.getOutputStream().write(be)
if(connection.getResponseCode()==200){
InputStream is=connection.getInputStream()
byte[] b=new byte[1024]
ByteArrayOutputStream bo=new ByteArrayOutputStream()
int length=0
while((length=is.read(b))!=-1){
bo.write(b, 0, length)
}
String st=bo.toString("gbk")
String string=st.substring(st.indexOf("{"))
System.out.println(string)
jiexi(string)
}
HttpClient
get
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(params[0]);
HttpResponse response=client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
InputStream is=response.getEntity().getContent();
return tojsonutils.getjson(is);
}
Post
HttpClient client=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(path);
List<BasicNameValuePair> parameters=new ArrayList<BasicNameValuePair>() ;
parameters.add(new BasicNameValuePair("info", et.getText().toString()));
parameters.add(new BasicNameValuePair("key", "65e2651ed3d6c03fb7172df8059578b8"));
HttpEntity entity=new UrlEncodedFormEntity(parameters, "utf-8");
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
InputStream is=response.getEntity().getContent();
String data=httputils.parseStream(is);
System.out.println(data);
Gson gson=new Gson();
bean be=gson.fromJson(data, bean.class);
list.add(be.result);
for(Result1 r:list){
str=r.text;
System.out.println("!!!!!!!!!!!!!!"+str);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(str);
}
});
}