使用HTTP协议访问网络

来源:互联网 发布:分身软件 编辑:程序博客网 时间:2024/06/14 16:33

它的工作原理特别的简单,就是客户端向服务器发出一条HTTP请求,服务器收到请求之后会返回一些数据给客户端,然后客户端再对这些数据进行解析和处理就可以了。

Android上发送HTTP请求的方式一般有两种,HttpURLConnectionHttpClient


1、使用HTTPURLConnection

首先需要获取到HttpURLConnection的实例,一般只需new出一个URL对象,并传入目标的网络地址,然后调用一下openConnection()方法即可。

得到了HttpURLConnection的实例之后,我们可以设置一下HTTP请求所使用的方法。常用的方法主要有两个,GETPOSTGET表示希望从服务器那里获取数据,而POST则表示希望提交数据给服务器。

接下来就可以进行一些自由的定制了,比如设置连接超时、读取超时的毫秒数,以及服务器希望得到的一些消息头等。

之后再调用getInputStream()方法就可以获取到服务器返回的输入流了,剩下的任务就是对输入流进行读。

之后再调用getInputStream()方法就可以获取到服务器返回的输入流了,剩下的任务就是对输入流进行读取。

最后可以调用disconnect()方法将这个HTTP连接关闭掉


activity_main.xml

<LinearLayout 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:orientation="vertical"        tools:context=".MainActivity" >    <Button        android:id="@+id/send_request"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Send Request" />    <ScrollView                android:layout_width="match_parent"        android:layout_height="match_parent" >        <TextView            android:id="@+id/response_text"            android:layout_width="match_parent"            android:layout_height="wrap_content"             />           </ScrollView></LinearLayout>

注意这里我们使用了一个新的控件,ScrollView,它是用来做什么的呢?由于手机屏幕的空间一般都比较小,有些时候过多的内容一屏是显示不下的,借助ScrollView控件的话就可以允许我们以滚动的形式查看屏幕外的那部分内容。另外,布局中还放置了一个Button和一个TextViewButton用于发送HTTP请求,TextView用于将服务器返回的数据显示出来。

MainActivity.java

package cn.bzu.bztc.networktest;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener{public static final int SHOW_RESPONSE=0;private Button sendRequest;private TextView responseText;private Handler handler=new Handler(){public void handleMessage(Message msg){switch (msg.what){case SHOW_RESPONSE:String response=(String) msg.obj;responseText.setText(response);}}};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendRequest=(Button) findViewById(R.id.send_request);        responseText=(TextView) findViewById(R.id.response_text);        sendRequest.setOnClickListener(this);    }    @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;    }@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v.getId()==R.id.send_request){sendRequestWithHttpURLConnection();}}private void sendRequestWithHttpURLConnection(){new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubHttpURLConnection connection=null;try {URL url=new URL("Http://www.baidu.com");connection =(HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.setConnectTimeout(8000);connection.setReadTimeout(8000);InputStream in =connection.getInputStream();BufferedReader reader=new BufferedReader(new InputStreamReader(in));StringBuilder response=new StringBuilder();String line;while((line=reader.readLine())!=null){response.append(line);}Message message=new Message();message.what=SHOW_RESPONSE;message.obj=response.toString();handler.sendMessage(message);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(connection!=null){connection.disconnect();}}}}).start();}    }


可以看到,我们在Send Request按钮的点击事件里调用了sendRequestWithHttpURL-Connection()方法,在这个方法中先是开启了一个子线程,然后在子线程里使用HttpURLConnection发出一条HTTP请求,请求的目标地址就是百度的首页。接着利用BufferedReader对服务器返回的流进行读取,并将结果存放到了一个Message对象中。这里为什么要使用Message对象呢?当然是因为子线程中无法对UI进行操作了。我们希望可以将服务器返回的内容显示到界面上,所以就创建了一个Message对象,并使用Handler将它发送出去。之后又在HandlerhandleMessage()方法中对这条Message进行处理,最终取出结果并设置到TextView上。


那么如果是想要提交数据给服务器应该怎么办呢?其实也不复杂,只需要将HTTP请求的方法改成POST,并在获取输入流之前把要提交的数据写出即可。注意每条数据都要以键值对的形式存在,数据与数据之间用&符号隔开,比如说我们想要向服务器提交用户名和密码



2、使用HttpClient

HttpClientApache提供的HTTP网络访问接口,从一开始的时候就被引入到了AndroidAPI中。它可以完成和HttpURLConnection几乎一模一样的效果,但两者之间的用法却有较大的差别,那么我们自然要看一下HttpClient是如何使用的了。

首先你需要知道,HttpClient是一个接口,因此无法创建它的实例,通常情况下都会创建一个DefaultHttpClient的实例。

接下来如果想要发起一条GET请求,就可以创建一个HttpGet对象,并传入目标的网络地址,然后调用HttpClientexecute()方法即可。

如果是发起一条POST请求会比GET稍微复杂一点,我们需要创建一个HttpPost对象,并传入目标的网络地址。

然后通过一个NameValuePair集合来存放待提交的参数,并将这个参数集合传入到一个UrlEncodedFormEntity中,然后调用HttpPostsetEntity()方法将构建好的UrlEncodedFormEntity传入。

接下来的操作就和HttpGet一样了,调用HttpClientexecute()方法,并将HttpPost对象传入即可。

执行execute()方法之后会返回一个HttpResponse对象,服务器所返回的所有信息就会包含在这里面。通常情况下我们都会先取出服务器返回的状态码,如果等于200就说明请求和响应都成功了。

接下来在这个if判断的内部取出服务返回的具体内容,可以调用getEntity()方法获取到一个HttpEntity实例,然后再用EntityUtils.toString()这个静态方法将HttpEntity转换成字符串即可。

注意如果服务器返回的数据是带有中文的,直接调用EntityUtils.toString()方法进行转换会有乱码的情况出现,这个时候只需要在转换的时候将字符集指定成utf-8就可以了。

好了,基本的用法就是如此,接下来就让我们把NetworkTest这个项目改用HttpClient的方式再实现一遍吧。

MainActivity.java

package cn.bzu.bztc.networktest;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import org.apache.http.HttpEntity;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 org.apache.http.util.EntityUtils;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener{public static final int SHOW_RESPONSE=0;private Button sendRequest;private TextView responseText;private Handler handler=new Handler(){public void handleMessage(Message msg){switch (msg.what){case SHOW_RESPONSE:String response=(String) msg.obj;responseText.setText(response);}}};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendRequest=(Button) findViewById(R.id.send_request);        responseText=(TextView) findViewById(R.id.response_text);        sendRequest.setOnClickListener(this);    }    @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;    }@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v.getId()==R.id.send_request){}}private void sendRequestWithHttpClient(){new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubHttpURLConnection connection=null;try {HttpClient httpClient=new DefaultHttpClient();HttpGet httpGet=new HttpGet("http://www.baidu.com");HttpResponse httpResponse=httpClient.execute(httpGet);if(httpResponse.getStatusLine().getStatusCode()==200){HttpEntity entity=httpResponse.getEntity();String response=EntityUtils.toString(entity,"utf-8");Message message=new Message();message.what=SHOW_RESPONSE;message.obj=response.toString();handler.sendMessage(message);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}    }

这里我们并没有做太多的改动,只是添加了一个sendRequestWithHttpClient()方法,并在Send Request按钮的点击事件里去调用这个方法。在这个方法中同样还是先开启了一个子线程,然后在子线程里使用HttpClient发出一条HTTP请求,请求的目标地址还是百度的首页,HttpClient的用法也正如前面所介绍的一样。然后为了能让结果在界面上显示出来,这里仍然是将服务器返回的数据存放到了Message对象中,并用HandlerMessage发送出去。




 


0 0
原创粉丝点击