---
title: "Android开发中如何发送Get请求！"
author: "mmdev"
date: "2009-12-17T15:59:00Z"
tags: []
slug: "android-get"
draft: false
lastmod: "2012-12-05T22:00:30Z"
id: 1742125
url: "https://mmdev.iteye.com/blog/1742125"
---

在Android开发中，如果我们想从服务器获取数据，需要发送一个Get请求；如何在Android开发中发送这个Get请求呢，请参照下面的例子；

/\*URL可以随意改\*/  
String uriAPI = "[http://192.168.1.100:8080/test/test.jsp?u=wangyi&p=456](http://writeblog.csdn.net/link.php?url=http%3A%2F%2F192.168.1.100%3A8080%2Ftest%2Ftest.jsp%3Fu%3Dwangyi%26amp%3Bp%3D456)";  
/\*建立HTTP Get对象\*/  
HttpGet httpRequest = new HttpGet(uriAPI);  
try  
{  
/\*发送请求并等待响应\*/  
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);  
/\*若状态码为200 ok\*/  
if(httpResponse.getStatusLine().getStatusCode() == 200)  
{  
/\*读\*/  
String strResult = EntityUtils.toString(httpResponse.getEntity());  
/\*去没有用的字符\*/  
strResult = eregi\_replace("(/r/n|/r|/n|/n/r)","",strResult);  
mTextView1.setText(strResult);  
}  
else  
{  
mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());  
}  
}  
catch (ClientProtocolException e)  
{  
mTextView1.setText(e.getMessage().toString());  
e.printStackTrace();  
}  
catch (IOException e)  
{  
mTextView1.setText(e.getMessage().toString());  
e.printStackTrace();  
}  
catch (Exception e)  
{  
mTextView1.setText(e.getMessage().toString());  
e.printStackTrace();  
}

这是在Android开发中常用的发送Get请求的方式。