2016-05-13 12 views
0

Ich war HttpClient, um einen Webservice zu treffen und dann JSON-Antwort zu behandeln, aber jetzt ist es veraltet dann, wie Sie httpUrlConnection verwenden, um JSON Antwort von Webservice zu behandeln.Handle JSON Antwort von Webservice mit httpurlconnection

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

Antwort

0

können Sie versuchen, die HttpURLConnection Anfrage wie unter Beispiel:

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