/*URL可以随意改*/
String uriAPI = "http://192.168.1.100:8080/test/test.jsp?u=wangyi&p=456";
/*建立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请求
本文介绍了一个使用Java发送HTTP GET请求并处理响应的例子。通过具体的代码实现,展示了如何构造请求、接收响应,并对响应结果进行解析及显示。

被折叠的 条评论
为什么被折叠?



