【问题标题】:handle json response of webservice using httpurlconnection使用 httpurlconnection 处理 webservice 的 json 响应
【发布时间】:2016-09-09 08:27:43
【问题描述】:

我使用 HttpClient 来访问 web 服务,然后处理 json 响应,但现在它已被弃用,然后如何使用 httpUrlConnection 来处理 web 服务的 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);

【问题讨论】:

    标签: android web-services httpclient httpurlconnection


    【解决方案1】:

    您可以尝试以下示例的 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;
        }
    

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多