Android(五)数据存储之五网络数据交互 2
?3.ClientService类
package com.changcheng.web.client.service;
?
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import android.os.Environment;
import android.util.Log;
?
public class ClientService {
?
???????? private static final String TAG = "ClientService";
?
???????? // 以get方式发送请求
???????? public static void sendDataToServerByGet() throws Exception {
?????????????????? // 主机地址不可以设置为localhost或127.0.0.1,必须是本机或其他机器所在Internet网或局域网地址。
?????????????????? String path = "http://192.168.0.2:8080/AndroidWebServer/server.do?"
???????????????????????????????????? + "method=sendDataByGet&name=changcheng";
?????????????????? URL url = new URL(path);
?????????????????? HttpURLConnection conn = (HttpURLConnection) url.openConnection();
?????????????????? conn.setConnectTimeout(6 * 1000);
?????????????????? // 请求成功
?????????????????? if (conn.getResponseCode() == 200) {
??????????????????????????? // 获取服务器返回的数据
??????????????????????????? byte[] data = readStream(conn.getInputStream());
??????????????????????????? Log.i(TAG, new String(data, "UTF-8"));
?????????????????? }
???????? }
?
???????? // 以Post方式发送请求,面向HTTP协议编程
???????? public static void sendDataTOserverByPost() throws Exception {
?????????????????? String path = "http://192.168.0.2:8080/AndroidWebServer/server.do";
?????????????????? String params = "method=sendDataByPost&name=tingting";// 请求参数
?????????????????? byte[] data = params.getBytes();
?????????????????? URL url = new URL(path);
?????????????????? HttpURLConnection conn = (HttpURLConnection) url.openConnection();
?????????????????? conn.setConnectTimeout(6 * 1000);
?????????????????? conn.setDoOutput(true);// 发送POST请求必须设置允许输出
?????????????????? conn.setUseCaches(false);// 不使用Cache
?????????????????? conn.setRequestMethod("POST");
?????????????????? conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
?????????????????? conn.setRequestProperty("Charset", "UTF-8");
?????????????????? conn.setRequestProperty("Content-Length", String.valueOf(data.length));
?????????????????? conn.setRequestProperty("Content-Type",
???????????????????????????????????? "application/x-www-form-urlencoded");
?????????????????? DataOutputStream outStream = new DataOutputStream(conn
???????????????????????????????????? .getOutputStream());
?????????????????? outStream.write(data);// 以内容实体方式发送请求参数
?????????????????? outStream.flush();
?????????????????? outStream.close();
?????????????????? // 请求成功
?????????????????? if (conn.getResponseCode() == 200) {
??????????????????????????? // 获取服务器返回的数据
??????????????????????????? byte[] html = readStream(conn.getInputStream());
??????????????????????????? Log.i(TAG, new String(html, "UTF-8"));
?????????????????? }
???????? }
?
???????? // 以表单方式发送请求
???????? public static void sendDataToServerByForm() throws Exception {
?????????????????? Map<String, String> params = new HashMap<String, String>();
?????????????????? params.put("method", "sendDataByForm");
?????????????????? params.put("strData", "字符串数据");
?????????????????? // 获取SDCard中的good.jpg
?????????????????? File file = new File(Environment.getExternalStorageDirectory(),
???????????????????????????????????? "app_Goog_Android_w.png");
?????????????????? FormFile fileData = new FormFile("app_Goog_Android_w.png", new FileInputStream(file),
???????????????????????????????????? "fileData", "application/octet-stream");
?????????????????? HttpRequester.post(
???????????????????????????????????? "http://192.168.0.2:8080/AndroidWebServer/server.do", params,
???????????????????????????????????? fileData);
???????? }
?
???????? // 获取输入流数据
???????? private static byte[] readStream(InputStream inStream) throws Exception {
?????????????????? byte[] buffer = new byte[1024];
?????????????????? int len = -1;
?????????????????? ByteArrayOutputStream outStream = new ByteArrayOutputStream();
?????????????????? while ((len = inStream.read(buffer)) != -1) {
??????????????????????????? outStream.write(buffer, 0, len);
?????????????????? }
?????????????????? byte[] data = outStream.toByteArray();
?????????????????? outStream.close();
?????????????????? inStream.close();
?????????????????? return data;
???????? }
}
?????? 其中使用到的FormFile类:
package com.changcheng.web.client.service;
?
import java.io.InputStream;
?
/**
?* 上传文件
?*/
public class FormFile {
???????? /* 上传文件的数据 */
???????? private byte[] data;
???????? private InputStream inStream;
???????? /* 文件名称 */
???????? private String filname;
???????? /* 表单字段名称*/
???????? private String formname;
???????? /* 内容类型 */
???????? private String contentType = "application/octet-stream";
????????
???????? public FormFile(String filname, byte[] data, String formname, String contentType) {
?????????????????? this.data = data;
?????????????????? this.filname = filname;
?????????????????? this.formname = formname;
?????????????????? if(contentType!=null) this.contentType = contentType;
???????? }
????????
???????? public FormFile(String filname, InputStream inStream, String formname, String contentType) {
?????????????????? this.filname = filname;
?????????????????? this.formname = formname;
?????????????????? this.inStream = inStream;
?????????????????? if(contentType!=null) this.contentType = contentType;
???????? }
????????
???????? public InputStream getInStream() {
?????????????????? return inStream;
???????? }
?
???????? public void setInStream(InputStream inStream) {
?????????????????? this.inStream = inStream;
???????? }
?
???????? public byte[] getData() {
?????????????????? return data;
???????? }
?
???????? public void setData(byte[] data) {
?????????????????? this.data = data;
???????? }
?
???????? public String getFilname() {
?????????????????? return filname;
???????? }
?
???????? public void setFilname(String filname) {
?????????????????? this.filname = filname;
???????? }
?
???????? public String getFormname() {
?????????????????? return formname;
???????? }
?
???????? public void setFormname(String formname) {
?????????????????? this.formname = formname;
???????? }
?
???????? public String getContentType() {
?????????????????? return contentType;
???????? }
?
???????? public void setContentType(String contentType) {
?????????????????? this.contentType = contentType;
???????? }
????????
}
?
??????