使用httpurlconnection处理webservice的json响应

时间:2016-05-13 08:25:55

标签: android web-services httpclient httpurlconnection

我正在使用HttpClient来访问web服务,然后处理json响应,但现在不推荐使用httpUrlConnection来处理webservice的json响应。

HttpClient httpClient = new DefaultHttpClient();
                String authenticationURL = "http://192.168.100.27:8080/NotificationWebService/saveDeviceID?userName="+username+"&deviceId="+regId+"&idType=ANDROID&callBack=jsonCallback";
                Log.d("RegisterActivity", "URL "
                        + authenticationURL);
                HttpGet authenticationGetRequest = new HttpGet(authenticationURL);
                HttpResponse authenticationResponse = httpClient.execute(authenticationGetRequest);

                Log.d("RegisterActivity", "registerInBackground - regId: "
                        + authenticationResponse);

1 个答案:

答案 0 :(得分:0)

您可以尝试HttpUrlConnection请求,如下例所示:

public String shareRegIdWithAppServer(final Context context,
            final String regId) {

        String result = "";
        Map<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put("regId", regId);
        try {
            URL serverUrl = null;
            try {
                serverUrl = new URL(Config.APP_SERVER_URL);
            } catch (MalformedURLException e) {
                Log.e("AppUtil", "URL Connection Error: "
                        + Config.APP_SERVER_URL, e);
                result = "Invalid URL: " + Config.APP_SERVER_URL;
            }

            StringBuilder postBody = new StringBuilder();
            Iterator<Entry<String, String>> iterator = paramsMap.entrySet()
                    .iterator();

            while (iterator.hasNext()) {
                Entry<String, String> param = iterator.next();
                postBody.append(param.getKey()).append('=')
                        .append(param.getValue());
                if (iterator.hasNext()) {
                    postBody.append('&');
                }
            }
            String body = postBody.toString();
            byte[] bytes = body.getBytes();
            HttpURLConnection httpCon = null;
            try {
                httpCon = (HttpURLConnection) serverUrl.openConnection();
                httpCon.setDoOutput(true);
                httpCon.setUseCaches(false);
                httpCon.setFixedLengthStreamingMode(bytes.length);
                httpCon.setRequestMethod("POST");
                httpCon.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded;charset=UTF-8");
                OutputStream out = httpCon.getOutputStream();
                out.write(bytes);
                out.close();

                int status = httpCon.getResponseCode();
                if (status == 200) {
                    result = "RegId shared with Application Server. RegId: "
                            + regId;
                } else {
                    result = "Post Failure." + " Status: " + status;
                }
            } finally {
                if (httpCon != null) {
                    httpCon.disconnect();
                }
            }

        } catch (IOException e) {
            result = "Post Failure. Error in sharing with App Server.";
            Log.e("AppUtil", "Error in sharing with App Server: " + e);
        }
        return result;
    }