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());
}
}
@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());
}
}
相关文章推荐
- Android网络编程之Http请求服务器数据(GET方式)
- Android传统HTTP请求get----post方式提交数据(包括乱码问题)
- android发送HTTP请求代码,以GET方式发送。
- Android中使用Volley开源库进行Http网络请求(GET方式)
- android的http请求get和post请求方式
- Url请求长度HTTP协议不对URI的长度作事先的限制,服务器必须能够处理任何他们提供资源的URI,并且应该能够处理无限长度的URIs,这种无效长度的URL可能会在客户端以基于GET方式的请求时产生。
- Android中使用Volley开源库进行Http网络请求(GET方式)
- Android中两种请求方式HttpGet和HttpPost验证用户登录
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- Android网络请求,Get方式请求,HttpURLConnection请求工具类
- Android传统HTTP请求get----post方式提交数据(包含乱码问题)
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- Android中的post和get请求(HttpURLConnection)方式
- Android网络编程之Http请求服务器数据(GET方式)
- HTTP请求方式GET和POST的区别详解
- HTTP请求方式GET和POST的区别详解
- Android Http get post请求
- android http get请求中的乱码问题
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- Android----用HTTP的get,post,HttpClient三种方式向web service提交文本数据