2012-11-13 75 views
0

我使用JSON從我的服務器獲取響應。 這是代碼:JSON在android中合併兩個響應

HttpClient httpclient = new DefaultHttpClient(); 
     HttpClient httpclient2 = new DefaultHttpClient(); 
     HttpResponse response; 
     HttpResponse response2; 
     try { 
      HttpGet request = new HttpGet(GlobalConfig.getMagazineUrl()); 
      HttpGet request2 = new HttpGet(GlobalConfig.getMagazinePagesUrl(1)); 

      request.addHeader("Authorization", "Basic " + Base64.encodeToString(
              (GlobalConfig.getAuthString()).getBytes(),Base64.NO_WRAP)); 
      request2.addHeader("Authorization", "Basic " + Base64.encodeToString(
        (GlobalConfig.getAuthString()).getBytes(),Base64.NO_WRAP)); 
      response = httpclient.execute(request); 
      StatusLine statusLine = response.getStatusLine(); 
      response2 = httpclient2.execute(request2); 
      StatusLine statusLine2 = response2.getStatusLine(); 

      if(statusLine.getStatusCode() == HttpStatus.SC_OK && statusLine2.getStatusCode() == HttpStatus.SC_OK){ 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       ByteArrayOutputStream out2 = new ByteArrayOutputStream(); 

       response.getEntity().writeTo(out); 
       response2.getEntity().writeTo(out2); 

       out.close(); 
       out2.close(); 
       return parser(out.toString(), out2.toString()); 

正如你可以看到parser(out.toString(), out2.toString())我回到這兩種響應爲String。我想知道如何將這兩個JSON響應合併爲一個。我不想合併兩個字符串,我需要在一個大響應中合併兩個JSON響應。這個有可能?如果是的話我該怎麼做?

回答

3

也許這是你想要的東西:

... 
JSONObject json = new JSONObject(); 
json.put("response1", new JSONObject(out.toString())); 
json.put("response2", new JSONObject(out2.toString())); 

現在回報取決於返回類型要麼json.toString()json