Android网络编程之Http通信

嵌入式系统 时间:2016-09-12来源:网络

//请求httpRequest

httpRequest.setEntity(httpentity);

//取得默认的HttpClient

HttpClient httpclient = new DefaultHttpClient();

//取得HttpResponse

HttpResponse httpResponse = httpclient.execute(httpRequest);

//HttpStatus.SC_OK表示连接成功

if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)

{

//取得返回的字符串

String strResult = EntityUtils.toString(httpResponse.getEntity());

mTextView.setText(strResult);

}

else

{

mTextView.setText(请求错误!);

}

}

HttpClient实际上是对Java提供方法的一些封装,在HttpURLConnection中的输入输出流操作,在这个接口中被统一封装成了HttpPost(HttpGet)和HttpResponse,这样,就减少了操作的繁琐性。

另外,在使用POST方式进行传输时,需要进行字符编码。

1 2

关键词:

加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW

或用微信扫描左侧二维码

相关文章

查看电脑版