Android开发之HttpClient初步
2016-05-16 19:22
411 查看
步骤:1.生成代表客户端的HttpClient对象;2.生成代表请求的HttpGet对象3.发送请求,获得服务器返回的HttpResponse对象4.检查响应状态是否正常5.获取响应对象中的数据
//activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/buttonId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="向服务器发送数据" /> </RelativeLayout> //MainActivity.java package com.example.http1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BufferedHeader; import org.xml.sax.InputSource; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.buttonId); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { //原则:在主线程中不可以访问网络 NetworkThread nt=new NetworkThread(); nt.start(); } }); } class NetworkThread extends Thread{ public void run(){ //创建HttpClietn对象 HttpClient httpClient = new DefaultHttpClient(); //创建代表请求的对象,参数是访问的服务器地址 HttpGet httpGet = new HttpGet("http://www.marschen.com/data1.html"); //执行请求,获取服务器发还的响应对象 try { HttpResponse resp=httpClient.execute(httpGet); int code = resp.getStatusLine().getStatusCode(); //检查状态码的值是否正常,检查状态码的值是否等于200 if (code==200) { //从响应中取出数据 HttpEntity entity = resp.getEntity(); InputStream in = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line=reader.readLine(); Log.d("HTTP","从服务器取得的数据为:"+line); } } catch (Exception e) { e.printStackTrace(); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } //AndroidManifest.xml注册<uses-permission android:name="android.permission.INTERNET"/>
相关文章推荐
- Unix网络编程之读写操作recv和send函数
- 计算机网络(原书第六版) 第二章笔记
- 【网络】TCP协议
- Git使用http clone客户端保存用户名密码
- python的http服务
- python的http服务
- 网络编程学习笔记一:Socket编程
- linux socket网络编程详解
- when i run server i got "name 'http' is not defined" after import h 4000 ttp.server then i got this "no mo
- 关于启用 HTTPS 的一些经验分享(二)
- when i run server i got "name 'http' is not defined" after import h 4000 ttp.server then i got this "no mo
- TCP/IP四层模型 (之前都是接触的七层模型)
- Android开发本地及网络Mp3音乐播放器(十九)通知媒体库更新刚下载的MP3
- Android开发本地及网络Mp3音乐播放器(十九)通知媒体库更新刚下载的MP3
- 嵌入式linux的网络编程(2)--TCP Server程序设计
- android studio 2.0 Gradle HttpProxy 设置
- UNIX网络编程之管道与FIFO
- 关于启用 HTTPS 的一些经验分享(一)
- 五十五 网络编程 UDP编程
- HTTP请求报文和HTTP响应报文