android httpGet请求,红色部分很关键,可能这种方式不是最好的。

来源:互联网 发布:月工资算法公式 编辑:程序博客网 时间:2024/06/05 23:57

android httpGet请求,红色部分很关键,可能这种方式不是最好的。
@SuppressLint("NewApi")
public void SendData() {
// TextView resultText = (TextView) this.findViewById(R.id.resultText);
String httpUrl = "http://www.baidu.com/s?wd=123";
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

// 创建httpRequest对象
HttpGet httpRequest = new HttpGet(httpUrl);
try
{
// 取得HttpClient对象
HttpClient httpclient = new DefaultHttpClient();
// 请求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
// 请求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
// 取得返回的字符串
String strResult = EntityUtils.toString(httpResponse.getEntity());
Log.d("strResult", "strResult" + strResult);
}
else
{
Log.d("error", "error");
}
}
catch (ClientProtocolException e)
{
Log.d("error", "error" + e.getMessage().toString());
}
catch (IOException e)
{
Log.d("error", "error" + e.getMessage().toString());
}
catch (Exception e)
{
Log.d("error", "error" + e.getMessage().toString());
}
}
0 0