Android 网络与通信学习过程

一、Android平台有3种网络接口可以使用,它们分别是:java.net.*,org.apache,android.net.*。

(1)使用java.net.*包连接网络:

		try {
			URL url = new URL("http://...");//定义地址
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();//打开连接
			if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
				InputStream is = conn.getInputStream();//获取数据
				//。。。处理数据
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

(2)Apache接口,使用android.net.http.*连接网络 :

		try {
			HttpClient http = new DefaultHttpClient();//创建HttpClient实例
			HttpGet get = new HttpGet("http://...");//创建HttpGet实例
			HttpResponse rp = http.execute(get);//连接
			if (rp.getStatusLine().getStatusCode()==HttpStatus.SC_OK) {
				InputStream is = rp.getEntity().getContent();
				//...处理数据
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

(3)Android网络接口,socket连接:

		InetAddress inetAddress;
		try {
			inetAddress = InetAddress.getByName("192.168.1.1");//IP地址
			Socket client = new Socket(inetAddress,5455,true);//接口
			InputStream in = client.getInputStream();
			OutputStream out = client.getOutputStream();
			//...处理
			in.close();
			out.close();
			client.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}

二、HTTP通信

Http:(Hyper Text Transfer protocol)超文本传输协议,基于请求/响应的模式、无状态协议,可靠传输,通常发生在TCP/IP连接之上,在网络层!Adnroid提供了HttpURLConnection与HttpClient接口来开发HTTP程序。

Http通信中使用最多的是Post和Get。get在请求静态页面时,可以把参数放在URL后面;post是放在HTTP请求的数据中。

HttpURLConnection类继承自URLConnection类,他们都是抽象类,不能被直接实例化对象,通过openConnection获得。

不要忘记加权限:

<uses-permission android:name="android.permission.INTERNET" />
		try {
			URL url = new URL("http://www.baidu.com");//定义地址
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();//打开连接
			if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
				InputStream is = conn.getInputStream();//获取数据
				//。。。处理数据
				InputStreamReader reader = new InputStreamReader(is);
				BufferedReader buffer = new BufferedReader(reader);
				String data,result = null;
				while((data=buffer.readLine())!=null){
					result+=data+"\n";
					data="";
				}
				textview.setText(result);
			}else{
				System.out.println("网络连接失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

HttpURLConnection默认是get方式,关于post方式,这里暂不做解释!


三、Http Post参数请求,返回json数据,并解析

				String url = "http://...";
				HttpPost request = new HttpPost(url);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("obj",
						"busBox.replacementBus"));
				params.add(new BasicNameValuePair("city", "1"));
				params.add(new BasicNameValuePair("1路", "四惠|西单路口东"));

				try {
					HttpEntity entity = new UrlEncodedFormEntity(params,
							"UTF-8");
					request.setEntity(entity);
					HttpClient client = new DefaultHttpClient();
					HttpResponse response = client.execute(request);
					if (response.getStatusLine().getStatusCode() == 200) {
						String result = EntityUtils.toString(response
								.getEntity());
						JSONObject json = new JSONObject(result);
						System.out.println(result);
						System.out.println(json.getJSONObject("data").getJSONArray("1路").getJSONObject(1).getString("name"));
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

Android 网络与通信学习过程_第1张图片

你可能感兴趣的:(Android 网络与通信学习过程)