Post方法访问网络数据(java)

在我的朋友 鹏军,的支持下做成的网络连接方法 

 

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值