网络编程
HttpGet,HttpPost
HttpURLConnection
Socket
HttpGet,HttpPost
使用步骤
创建对象,将URL传入对象 > 使用DefaultHttpClient.execute发送请求,返回HttpResponse对象 >
>通过HttpRespnse接口的getEntity方法返回响应信息,并进行处理。
(如果使用HttpPost方法提交HTTP POST请求,还需要使用HttpPost类的setEntity方法设置请求参数)
HttpURLConnection
1,Url.openConnection获取HttpURLConnection对象
2,设置请求方法httpURLConnection.setRequestMethond("POST");
3,设置输入输出权限
4,设置HTTP请求头
5,输入输出数据;输入(从服务端读取),getInputStream();
6,关闭输入输出流,connection.disconnect();
发送HTTP请求
HttpURLConnection,HttpClient
new URL对象,传入网络地址
HttpGet,HttpPost
HttpURLConnection
Socket
HttpGet,HttpPost
使用步骤
创建对象,将URL传入对象 > 使用DefaultHttpClient.execute发送请求,返回HttpResponse对象 >
>通过HttpRespnse接口的getEntity方法返回响应信息,并进行处理。
(如果使用HttpPost方法提交HTTP POST请求,还需要使用HttpPost类的setEntity方法设置请求参数)
HttpURLConnection
1,Url.openConnection获取HttpURLConnection对象
2,设置请求方法httpURLConnection.setRequestMethond("POST");
3,设置输入输出权限
4,设置HTTP请求头
5,输入输出数据;输入(从服务端读取),getInputStream();
6,关闭输入输出流,connection.disconnect();
发送HTTP请求
HttpURLConnection,HttpClient
new URL对象,传入网络地址
<uses-permission android:name="android.permission.INTERNET"/>
2,布局activity_main.xml...ScrollView允许我们以滚动的形式查看屏幕外的那部分内容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/send_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Request"/> <ScrollViewandroid: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>3,MainActivity
package com.sammer.networktest; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class MainActivity extends Activity implements View.OnClickListener { public static final int SHOW_RESPONSE = 0 ; private Button sendRequest; private TextView responseText; private Handler handler = new Handler(){ @Override 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 void onClick(View view) { if(view.getId() == R.id.send_request) { sendRequestWithHttpUrlConnection(); } } private void sendRequestWithHttpUrlConnection() { //开启线程发送网络请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL("http://www.baidu.com"); //使用HttpsURLConnection发送网络请求,返回结果在connection connection = (HttpURLConnection) url.openConnection(); //connection.setRequestMethod("POST"); connection.setRequestMethod("POST"); connection.setConnectTimeout(8000); connection.setConnectTimeout(8000); InputStream in = connection.getInputStream(); //读取获取到的输入流,in服务器返回的流 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中,子线程无法对UI进行操作 message.obj = response.toString(); handler.sendMessage(message);//显示到屏幕上 } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }