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