2015-12-08 50 views
1

我有以下帶有base64圖像內容的JSON字符串。你能幫助我,我怎麼能張貼此JSON作爲多部分:Android使用POST方法向服務器傳遞JSON數據

{ 
    "TakeoffID": "2", 
    "address": ",nexhex", 
    "city": "Xrk Zed", 
    "state": "AZ", 
    "date": "12/08/2015", 
    "ViewNote": "", 
    "ViewPhoto1": "base64ImageContent", 
    "ViewPhoto2": "base64ImageContent", 
    "ViewPhoto3": "base64ImageContent", 
    "TakeoffDoneBy": "Jxehx", 
    "AcctName": "Gsgve", 
    "LoginUserID": "46669", 
    "jobId": "whshs", 
    "LineItems": [ 
     { 
      "OrderLineid": "544", 
      "OrderLineTypeid": "Post Light", 
      "OrderLineQty": "2", 
      "OrderLinePhoto1": "base64ImageContent", 
      "OrderLinePhoto2": "base64ImageContent", 
      "OrderLinePhoto3": "base64ImageContent", 
      "OrderLineNotes": "", 
      "OrderLineLocation": "Post Lights" 
     } 
    ] 
} 
+0

由於它是Base64編碼圖像,您不應該只發送一個普通的'application/json'請求嗎?去[OkHttp食譜](https://github.com/square/okhttp/wiki/Recipes)。它擁有你所需要的一切。 –

+0

確實。將json作爲多部分發送是沒有意義的。 – greenapps

+0

@Ravi,你有沒有試過下面的答案,它工作嗎? – Hits

回答

1

一個簡單的方法來做到這一點,首先將你的要求JSON轉換成簡單的地圖就像

Map<String, String> map = new HashMap<>(); 

對於「了LineItem」:「 LineItems「作爲鍵,它將json作爲字符串格式進行賦值並添加到此映射中。

然後使用下面的方法調用Webservice。

private JSONObject sendRequest(String urlString, Map<String, String> map, String fileKey, File file) { 
     StringBuilder strData= null; 
     JSONObject resObj = null; 
     try { 
      Log.i("Send request", urlString+"="+map); 
      URL url = new URL(urlString); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setReadTimeout(50000); 
      conn.setConnectTimeout(50000); 
      conn.setRequestMethod("POST"); 
      conn.setUseCaches(false); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 

      if(map == null) 
      { 
       map = new HashMap<>(); 
      } 
      MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
      for (HashMap.Entry<String, String> entry : map.entrySet()) { 
       String k = entry.getKey(); 
       String v = entry.getValue(); 
       reqEntity.addPart(k, new StringBody(v)); 
      } 

      if(file != null && !TextUtils.isEmpty(fileKey)) 
      { 
       FileBody filebody = new FileBody(file, "image/*"); 
       reqEntity.addPart(fileKey, filebody); 
      } 

      conn.setRequestProperty("Connection", "Keep-Alive"); 
      conn.addRequestProperty("Content-length", reqEntity.getContentLength() + ""); 
      conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue()); 
      OutputStream os = conn.getOutputStream(); 
      reqEntity.writeTo(os); 
      os.close(); 
      conn.connect(); 
      if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
       String sResponse; 
       strData = new StringBuilder(); 
       while ((sResponse = reader.readLine()) != null) { 
        strData = strData.append(sResponse); 
       } 
      } 
      if(strData != null) 
       resObj = new JSONObject(strData.toString()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return resObj; 
    } 
+0

你會解釋我,上面的方法將如何工作我的JSON字符串,因爲我共享JSONString – Philliphe

+0

您可以將您的上述JSON轉換爲地圖如: map.put(「TakeoffID」,「2」);依此類推......使用此地圖參考調用上述方法。 – Hits

+0

但我如何管理LineItems數組? – Philliphe

相關問題