Preface:
The following examples do not necessary work out of the box as we do not provide the required backend for receiving the data. Think of the following as examples how to setup HttpClients. Download the HttpClient libraries from the Apache Website, you can download the "bin" package it includes all dependencies.
Http Get:
The following is an example an HTTP Get request via DefaultHttpClient:
- HttpClient client = new DefaultHttpClient();
- HttpGet request = new HttpGet("http://www.vogella.com");
- HttpResponse response = client.execute(request);
- // Get the response
- BufferedReader rd = new BufferedReader
- (new InputStreamReader(response.getEntity().getContent()));
- String line = "";
- while ((line = rd.readLine()) != null) {
- textView.append(line);
- }
The following will send a post request to the URL "http://vogellac2dm.appspot.com/register" and include a parameter "registrationid" which an random value.
- package de.vogella.web.httpclient;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- public class SimpleHttpPut {
- public static void main(String[] args) {
- HttpClient client = new DefaultHttpClient();
- HttpPost post = new HttpPost("http://vogellac2dm.appspot.com/register");
- try {
- List
nameValuePairs = new ArrayList ( 1); - nameValuePairs.add(new BasicNameValuePair("registrationid",
- "123456789"));
- post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- HttpResponse response = client.execute(post);
- BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- String line = "";
- while ((line = rd.readLine()) != null) {
- System.out.println(line);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- package de.vogella.web.httpclient;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- public class TestHttpClient {
- public static void main(String[] args) {
- HttpClient client = new DefaultHttpClient();
- HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin");
- try {
- List
nameValuePairs = new ArrayList ( 1); - nameValuePairs.add(new BasicNameValuePair("Email", "youremail"));
- nameValuePairs
- .add(new BasicNameValuePair("Passwd", "yourpassword"));
- nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
- nameValuePairs.add(new BasicNameValuePair("source",
- "Google-cURL-Example"));
- nameValuePairs.add(new BasicNameValuePair("service", "ac2dm"));
- post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- HttpResponse response = client.execute(post);
- BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- String line = "";
- while ((line = rd.readLine()) != null) {
- System.out.println(line);
- if (line.startsWith("Auth=")) {
- String key = line.substring(5);
- // do something with the key
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
* 符碼記憶 - Apache HttpClient 4.x 使用 GET, POST 範例
沒有留言:
張貼留言