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

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;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值