android使用apache httpclient項目實現手機做爲http客戶端的調用。android使用的是最新的httpclient 4.0版本,網上不少國內的文檔是3.x的,調用方式不同。html
httpclient有一個官方教程,見:java
http://hc.apache.org/httpcomponents-client/tutorial/html/android
httpclient的javadoc,見:apache
http://hc.apache.org/httpcomponents-client/httpclient/apidocs/overview-summary.htmlapi
可在activivy中直接調用httpclient作對服務器端的訪問,如下是一個簡單的示例代碼:服務器
package com.easymorse;app
import java.io.BufferedReader;
import java.io.InputStreamReader;ideimport org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;thisimport android.app.Activity;
import android.os.Bundle;
import android.util.Log;componentpublic class NextActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.next_activity);HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(「http://marshal.easymorse.com/」);
try {
HttpResponse response = client.execute(get);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
Log.v(「response」, s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
代碼是在實現android activity之間的跳轉示例上修改的。