2015-11-19 29 views
1

我正在尝试执行杰克逊库来首次解析Json。我应该在我的HTTPClass中实现这个杰克逊根元素代码?我还需要对我的HTTPClass进行哪些其他更改?我应该在我的HTTPClass中实现这个杰克逊根元素代码?

从服务器获取的Json 杰克逊代码

RootElement rootElement = null; 
    ObjectMapper objectMapper = new ObjectMapper(); 
    objectMapper.configure(Feature.AUTO_CLOSE_SOURCE, true); 
    // this line is very important 
    objectMapper.configure(

DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); 

try { 
     rootElement = objectMapper.readValue(result, RootElement.class); 
    } catch (JsonParseException e) { 
      e.printStackTrace(); 
    } catch (JsonMappingException e) { 
      e.printStackTrace(); 
    } 

HTTPClass

... 
public static void getDailyStorylineList(final MovesHandler<ArrayList<StorylineData>> handler, 
               final String specificSummary, 
               final String from, 
               final String to, 
               final String pastDays, 
               final String updatedSince, 
               final boolean needTrackPoints) { 
      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         /* Refresh access token if only AuthData.MOVES_REFRESHBEFORE days are there to expire current token */ 
         AuthData.refreshAccessTokenIfNeeded(); 

        /* Exchange the authorization code we obtained after login to get access token */ 
        HashMap<String, String> nameValuePairs = new HashMap<String, String>(); 
        nameValuePairs.put("access_token", AuthData.getAuthData().getAccessToken()); 

        // if (specificSummary != null && specificSummary.length() > 0) nameValuePairs.put("specificSummary", specificSummary);//att 

        if (from != null && from.length() > 0) nameValuePairs.put("from", from); 
        if (to != null && to.length() > 0) nameValuePairs.put("to", to); 
        if (pastDays != null && pastDays.length() > 0) nameValuePairs.put("pastDays", pastDays); 
        if (updatedSince != null && updatedSince.length() > 0) nameValuePairs.put("updatedSince", updatedSince); 
        if (needTrackPoints) nameValuePairs.put("trackPoints", "true"); 


        URL url  = new URL(MovesAPI.API_BASE + MovesAPI.API_PATH_STORYLINE + (specificSummary != null ? specificSummary : "") + "?" + Utilities.encodeUrl(nameValuePairs)); 
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
        urlConnection.setRequestMethod("GET"); 
        urlConnection.setDoInput(true); 
        urlConnection.connect(); 

        if (urlConnection.getResponseCode() != 200) { 
         /* All other HTTP errors from Moves will fall here */ 
         handler.onFailure(getErrorStatus(Utilities.readStream(urlConnection.getErrorStream()), urlConnection.getResponseCode()), "Server not responded with success ("+ urlConnection.getResponseCode() +")"); 
         return; 
        } 

        String response = Utilities.readStream(urlConnection.getInputStream()); 
        Object object = new JSONTokener(response).nextValue(); 
        if (object instanceof JSONArray) { 
         JSONArray jsonArray = (JSONArray) object; 
         ArrayList<StorylineData> storylineData = new ArrayList<StorylineData>(); 
         if (jsonArray != null) { 
          for (int i = 0; i < jsonArray.length(); i++) { 
           JSONObject storylineJsonObject = jsonArray.optJSONObject(i); 
           if (storylineJsonObject != null) { 
            storylineData.add(StorylineData.parse(storylineJsonObject)); 
           } 
          } 
         } 
         handler.onSuccess(storylineData); 
        } else { 
         handler.onFailure(MovesStatus.INVALID_RESPONSE, "Expected a JSONArray from server, but failed"); 
        } 

       } catch (Exception ex) { 
        ex.printStackTrace(); 
        handler.onFailure(MovesStatus.UNEXPECTED_ERROR, "An unexpected error occured, please check logcat"); 
       } 
      } 
     }).start(); 
    } 

回答

0

支票上的HTTP RC值

代码之前期运用字符串响应

你可以使用http.response.inputStream上的jackson API

byte[] response = new ByteArrayInputStream(httpResponse.getInputStream()) 

try { 
    JsonNode _node = new ObjectMapper().readValue(new ByteArrayInputStream(response), JsonNode.class); 
// ref elements within RootObj that r needed       
    String itmId = node.path("objectId").getTextValue(); 
    }