Android 访问http实例

package eoe.demo;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public 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();
}
}
}

可在activivy中直接调用httpclient做对服务器端的访问,以下是一个简单的示例代码:


你可能感兴趣的:(Android 访问http实例)