[android]网络通信demo

项目说明.txt 外部引用原始文档
1
本demo简单的实现了网络通信中的常用的方法,介绍了get方法和post方法的使用,通信api分两种,一种是apache的和java自带的http的。喜欢的同学可以看看。
MainActivity.java 外部引用原始文档
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.eoe.eoehttp;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        
        btn1.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				connect("http://www.eoeandroid.com");
			}
        });
        
        btn2.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				 connectPost("http://192.168.0.37:6666/text/index.php");    //测试地址
			}
        });
        
        btn3.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				 javaHttpGet("http://www.eoeandroid.com");
			}
        });
        
        btn4.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				javaHttpPost("http://192.168.0.37:6666/text/index.php");   //测试地址
			}
        });
         
    }

    private void connect (String url){
    	HttpClient httpClient = new DefaultHttpClient();   //新建HttpClient对象
    	HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 3000); //设置连接超时
    	HttpConnectionParams.setSoTimeout(httpClient.getParams(), 3000); //设置数据读取时间超时
    	ConnManagerParams.setTimeout(httpClient.getParams(), 3000);  //设置从连接池中取连接超时
    	
    	
    	HttpGet httpget = new HttpGet(url);  //获取请求
    	
    	try {
    		HttpResponse response = httpClient.execute(httpget);     //执行请求,获取响应结果
    		if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  //响应通过
    			String result = EntityUtils.toString(response.getEntity(), "UTF-8");   
    			Intent httpclientIntent = new Intent();
    			httpclientIntent.putExtra("content", result);
    			httpclientIntent.setClass(getApplicationContext(), detailActivity.class);
    			startActivity(httpclientIntent);
    		}else{
    			                                               //响应未通过
    		}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    }
    
    private void connectPost(String url){
    	
    	  HttpClient httpClient = new DefaultHttpClient();    // 新建HttpClient对象
    	  HttpPost httpPost = new HttpPost(url);    // 新建HttpPost对象
    	  List<NameValuePair> params = new ArrayList<NameValuePair>();  //使用NameValuePair来保存要传递的Post参数
    	  params.add(new BasicNameValuePair("username", "hello"));    //添加要传递的参数
    	  params.add(new BasicNameValuePair("password", "eoe"));   
			try {
				 HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);  // 设置字符集
	    	     httpPost.setEntity(entity);    // 设置参数实体
	    	     HttpResponse httpResp = httpClient.execute(httpPost); // 获取HttpResponse实例
	    		if(httpResp.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  //响应通过
	    			String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");   
	    			
	    			Intent httpclientIntent = new Intent();
	    			httpclientIntent.putExtra("content", result);
	    			httpclientIntent.setClass(getApplicationContext(), detailActivity.class);
	    			startActivity(httpclientIntent);
	    		}else{
	    			                                               //响应未通过
	    		}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}   
    }
    
    private void javaHttpGet(String url){
    	try {
			URL pathUrl = new URL(url);    //创建一个URL对象
			HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection();  //打开一个HttpURLConnection连接
			urlConnect.setConnectTimeout(3000);  // 设置连接超时时间
			urlConnect.connect();
			InputStreamReader in = new InputStreamReader(urlConnect.getInputStream()); //得到读取的内容
			BufferedReader buffer = new BufferedReader(in);  //为输出创建BufferedReader
			String inputLine = null;
			String resultData = null;
			while (((inputLine = buffer.readLine()) != null)) {
				//利用循环来读取数据\
				resultData += inputLine;
			}
			Intent httpclientIntent = new Intent();
			httpclientIntent.putExtra("content", resultData);
			httpclientIntent.setClass(getApplicationContext(), detailActivity.class);
			startActivity(httpclientIntent);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    private void javaHttpPost(String url){
    	
    	try {
    		String params = "username=" + URLEncoder.encode("hello", "UTF-8")+ "&password=" + URLEncoder.encode("eoe", "UTF-8");
    		byte[] postData = params.getBytes();
    		URL pathUrl = new URL(url); //创建一个URL对象
			HttpURLConnection urlConnect = (HttpURLConnection) pathUrl.openConnection(); 
			urlConnect.setConnectTimeout(3000);  // 设置连接超时时间
			urlConnect.setDoOutput(true);  //post请求必须设置允许输出
			urlConnect.setUseCaches(false); //post请求不能使用缓存
			urlConnect.setRequestMethod("POST");  //设置post方式请求
			urlConnect.setInstanceFollowRedirects(true); 
			urlConnect.setRequestProperty("Content-Type","application/x-www-form-urlencode");// 配置请求Content-Type
			urlConnect.connect();  // 开始连接
			DataOutputStream dos = new DataOutputStream(urlConnect.getOutputStream()); // 发送请求参数
			dos.write(postData);
			dos.flush();
			dos.close();
			if (urlConnect.getResponseCode() == 200) {    //请求成功
				byte[] data = readInputStream(urlConnect.getInputStream());   
				
    			Intent httpclientIntent = new Intent();
    			httpclientIntent.putExtra("content", new String(data, "UTF-8"));
    			httpclientIntent.setClass(getApplicationContext(), detailActivity.class);
    			startActivity(httpclientIntent);

			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
    }
    
    public  byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len = inStream.read(buffer)) !=-1 ){
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();//转化为二进制数据
        outStream.close();
        inStream.close();
        return data;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值