您的位置:首页 > 理论基础 > 计算机网络

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

2014-12-12 10:40 295 查看
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());

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: