在我的朋友 鹏军,的支持下做成的网络连接方法
如requestStr为 {"action":"getCityByName","module":"city","cityName":"北京"} 就可以获得数据
/**
* 网络连接和获得网络字符串的方法
*
* @param requestStr 请求的字符串
* @return 返回的字符串
* @throws UnsupportedEncodingException
* @throws ClientProtocolException
* @throws IOException
*/
private static String executeRequest(String requestStr) throws UnsupportedEncodingException,
ClientProtocolException, IOException, ParseException, IllegalArgumentException,
MalformedURLException, SocketTimeoutException, ClientProtocolException {
client = new DefaultHttpClient();
httpParams = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
HttpConnectionParams.setSoTimeout(httpParams, 30000);
mHttpPost = new HttpPost(urlString);
httpParams.setParameter("client-version", "1.0"); // 发送客户端的版本
Log.d(TAG, "StaticInfo.IMEI=" + StaticInfo.IMEI);
httpParams.setParameter("client-id", StaticInfo.IMEI); // 发送客户端的手机卡号
httpParams.setParameter("client-system", "android"); // 发送客户端是android还是ophone
// Log.d(TAG, "-----------------------");
// Log.d(TAG, mHttpPost.toString());
nameValuePairs = new ArrayList<NameValuePair>(0);
nameValuePairs.add(new BasicNameValuePair("requestData", requestStr));
mHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
response = client.execute(mHttpPost);
entity = response.getEntity();
response.getHeaders("current-version");
response.getHeaders("upgrade-url");
// Header h = response.getFirstHeader("current-version");
// Log.i(TAG,"name=" + h.toString());
// Log.i(TAG,"name=" + h.getName());
// Log.i(TAG,"value=" + h.getValue());
String currentVersion = (String) response.getParams().getParameter("current-version");
String upgradeUrl = (String) response.getParams().getParameter("upgrade-url");
Log.i(TAG, "currentVersion = " + currentVersion);
Log.i(TAG, "upgradeUrl = " + upgradeUrl);
String responseStr = "";
if (entity != null) {
responseStr = EntityUtils.toString(response.getEntity());
}
return responseStr;
}
private static List<NameValuePair> nameValuePairs = null;
private static HttpClient client = null;
private static HttpParams httpParams = null;
private static HttpResponse response = null;
private static HttpPost mHttpPost = null;
private static HttpEntity entity = null;