android中使用get方法访问一个url

1.请求方法不能写在主线程中,要另开启一个线程,在主线程中开启子线程



import java.io.BufferedReader;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new MyThread().start();//启动了我们的线程了
}


class MyThread extends Thread {
public void run() {
// 你要实现的代码


String userId = "1";
String location = "北京市朝阳区";
String startoverTime = "2014-05-29 12:23:25";
String url = "http://*************************************";
String urlString = url + "&userId=" + userId + "&location=" + location
+ "&startoverTime=" + startoverTime;



System.out.println("urlString = " + urlString);


// 生成请求对象
HttpGet httpGet = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();


// 发送请求
try {


HttpResponse response = httpClient.execute(httpGet);


System.out.println("response = " + response);
// 显示响应
showResponseResult(response);// 一个私有方法,将响应结果显示出来


} catch (Exception e) {
e.printStackTrace();
}



}
}

/**
* 显示响应结果到命令行

* @param response
*/
private void showResponseResult(HttpResponse response) {
if (null == response) {
return;
}


HttpEntity httpEntity = response.getEntity();
try {
InputStream inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
String result = "";
String line = "";
while (null != (line = reader.readLine())) {
result += line;


}


System.out.println("result =" + result);
// mResult.setText("Response Content from server: " + result);
} catch (Exception e) {
e.printStackTrace();
}


}


}


2.第二种方法HttpConnection

===================================================================================================================

public class HttpConnection {
public static String getHttpRequest(String urlString){
URL url;
InputStream in = null;
HttpURLConnection conn = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setDoInput(true);
conn.connect();

if(conn.getResponseCode() == HttpsURLConnection.HTTP_OK){
in = conn.getInputStream();
int len =0;
byte[] buffer = new byte[1024];
while((len= in.read(buffer))!=-1){
out.write(buffer, 0, len);
}
                return out.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

if(conn!=null){
conn.disconnect();
}

if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
return null;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值