json数据转换后通过http向服务器发送数据_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > json数据转换后通过http向服务器发送数据

json数据转换后通过http向服务器发送数据

 2014/12/7 15:49:07  lhq1013  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.yunosauto.utils;importjava.io.IOException;importjava.net.InetAddress;importjava.util.Iterator;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpException;importorg.apache.commons.httpclient.HttpStatus
  • 标签:数据 服务器 JSON 服务 JS HTTP
class="java" name="code">package com.yunosauto.utils;


import java.io.IOException;
import java.net.InetAddress;
import java.util.Iterator;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.ddmlib.IDevice;


/**
 * 数据上传
 *
 */
public class UploadData {
	
	/**
	 * 上传数据至服务端
	 * @param url   服务器url地址
	 * @param iDevice    具体执行测试的手机对象
	 * @param jsonObj    结果数据
	 * @param type       测试类型,如“fps”
	 * @return
	 * @throws JSONException
	 */
	public static boolean uploadData(String url, IDevice iDevice, JSONObject jsonObj, String type) throws JSONException {
		PostMethod postMethod = new PostMethod(url);
		postMethod.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=utf-8");
		postMethod.addRequestHeader("Accept", "text/plain");
		
		int len = jsonObj.length();
		NameValuePair[] data = new NameValuePair[len + 7];
		Iterator it = jsonObj.keys();
		int i = 0;
		while(it.hasNext()) {
			String key = (String) it.next();
			NameValuePair tmpNameValuePair = new NameValuePair(key, jsonObj.get(key).toString());
			data[i] = tmpNameValuePair;
			i += 1;
		}
		
		NameValuePair tmpNameValuePair = new NameValuePair("type", type);
		data[len] = tmpNameValuePair;
		tmpNameValuePair = new NameValuePair("timestamp", Utils.getTimeStamp());
		data[len + 1] = tmpNameValuePair;
		tmpNameValuePair = new NameValuePair("username", getUsername());
		data[len + 2] = tmpNameValuePair;
		tmpNameValuePair = new NameValuePair("version", Device.getVersion(iDevice));
		data[len + 3] = tmpNameValuePair;
		tmpNameValuePair = new NameValuePair("hostip", getHostip());
		data[len + 4] = tmpNameValuePair;
		tmpNameValuePair = new NameValuePair("imei", Device.getDeviceImei(iDevice));
		data[len + 5] = tmpNameValuePair;
		tmpNameValuePair = new NameValuePair("device", Device.getDeviceName(iDevice));
		data[len + 6] = tmpNameValuePair;

		postMethod.setRequestBody(data);
		
		HttpClient httpClient = new HttpClient();
		int statusCode = 0;
		try {
			statusCode = httpClient.executeMethod(postMethod);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(" status code:" + statusCode);
		if (statusCode == HttpStatus.SC_OK) {
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
			return true;
		}
		return false;
	}
	
	/**
	 * 获取本地PC host ip.
	 * @return
	 */
	public static String getHostip() {
		String hostip = null;
		try {
			InetAddress ia = InetAddress.getLocalHost();
			hostip = ia.getHostAddress();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hostip;
	}

	/**
	 * 获取本地PC host name.
	 * @return
	 */
	public static String getHostname() {
		String hostname = null;
		try {
			InetAddress ia = InetAddress.getLocalHost();
			hostname = ia.getHostName();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hostname;
	}
	
	/**
	 * 获取本地PC host name.
	 * @return
	 */
	public static String getUsername() {
		String username = null;
		try {
			username = System.getProperty("user.name");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return username;
	}
}

?

  • org-json.jar (29.9 KB)
  • 下载次数: 1
  • org.apache.commons.httpclient.jar (300.3 KB)
  • 下载次数: 0
  • commons-logging-1.1.3.jar (60.6 KB)
  • 下载次数: 0
  • commons-codec-1.6.jar (227.3 KB)
  • 下载次数: 0
发表评论
用户名: 匿名