- public class HttpPostDemo extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- BufferedReader in = null;
- try {
- HttpClient client = new DefaultHttpClient();
- HttpPost request = new HttpPost("http://mysomewebsite.com/services/doSomething.do");
- List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
- postParameters.add(new BasicNameValuePair("username", "test"));
- postParameters.add(new BasicNameValuePair("password", "test1234"));
- UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
- postParameters);
- request.setEntity(formEntity);
- HttpResponse response = client.execute(request);
- in = new BufferedReader(
- new InputStreamReader(
- response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
- in.close();
- String result = sb.toString();
- System.out.println(result);
- } catch(Exception e) {
- // Do something about exceptions
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
HtttpGet 请求
在Android SDK中提供了Apache HttpClient(org.apache.http.*)模块。在这个模块中涉及到两个重要的类:HttpGet和HttpPost。这一篇一个实例给出httpGet的使用方法:
- public class HttpGetDemo extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- BufferedReader in = null;
- try {
- HttpClient client = new DefaultHttpClient();
- HttpGet request = new HttpGet("http://www.baidu.com");
- HttpResponse response = client.execute(request);
- in = new BufferedReader(
- new InputStreamReader(
- response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
- in.close();
- String page = sb.toString();
- System.out.println(page);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
在Android SDK中提供了Apache HttpClient(org.apache.http.*)模块。在这个模块中涉及到两个重要的类:HttpGet和HttpPost,他们有共性也有不同。
1、创建HttpGet(或HttpPost)对象,将要请求的URL通过构造方法传入HttpGet(或HttpPost)对象中;
2、使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST 请求,并返回HttpResponse对象;
3、通过HttpResponse接口的getEntity方法返回响应信息。
HttpGet和HttpPost不同点,HttpPost在使用是需要传递参数 ,使用List<NameValuePair>添加参数。
- <span style="font-weight: normal;"> List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
- postParameters.add(new BasicNameValuePair("username", "test"));
- postParameters.add(new BasicNameValuePair("password", "test1234"));</span>
- <span style="font-weight: normal;"> List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
- postParameters.add(new BasicNameValuePair("username", "test"));
- postParameters.add(new BasicNameValuePair("password", "test1234"));</span>