2014-09-03 66 views
0

問題:如何在多部分http POST上發送嵌入式關聯(name => value)子數組,而不僅僅是普通的單維頂級名稱=>值對?我搜查了很久,無法找到一個這樣的例子。在Android MultipartMode中發送多個嵌套的NameValue數組POST

+0

歡迎SO。你不應該發表一個答案作爲問題。如果您將原始問題作爲一個問題陳述,然後將解決方案作爲對該問題的答案發布,那將會更有意義。 – honk 2014-09-03 16:49:02

+0

感謝協議更正。我會嘗試繞回並重新格式化我的帖子,以符合問題然後回覆發佈格式。 -SM – FlannelTuba 2014-09-03 17:09:13

回答

1

這是我對這個問題的解決方案:如何使用MultipartEntityBuilder實體從Android設備發送multipart文章中的name => value對的子數組。這對我來說很有用。它還發送一個圖像文件,但其中的例子比比皆是,這裏唯一的是嵌套的嵌套關聯數組在相同的發佈請求中。我希望這可以幫助別人。如果您發現問題,請隨時指出。請原諒任何格式錯誤,因爲這是我第一次在這裏發帖。謝謝,斯科特

public String makePostRequest() { 

    public final static String INTENT_URL_PARAM = "server_url"; 
    public final static String INTENT_API_PARAM = "server_api"; 
    // Note: extras array is the incoming intent bundle, btw. 
    String url = extras.getString(INTENT_URL_PARAM); 
    String api = extras.getString(INTENT_API_PARAM); 
    url += "api/" + api; // add the api call to the base URL 

    HttpPost httpPost = new HttpPost(url); 

    String filePath = extras.getString("file_path"); 
    File file = new File(filePath); 
    if (!file.exists()) { 
     Log.i(TAG, getClass().getSimpleName() + "file failed exists(); filePath = " + filePath); 
    } 
    else { 
     FileBody fileBody = new FileBody(file); 
     try { 
      Log.i(TAG, getClass().getSimpleName() + ": try {putting together multipart post request}"); 
      HttpClient client = new DefaultHttpClient(); 
      MultipartEntityBuilder builder = MultipartEntityBuilder.create();   
      builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 
      // add image file 
      builder.addPart("file", fileBody); 
      // add additional post data 
      builder.addTextBody("user_id", extras.getString("user_id")); 
      // assemble the item[] and item_media[] array. 
      builder.addTextBody("item[name]", extras.getString("name")); 
      builder.addTextBody("item[description]", extras.getString("description")); 
      builder.addTextBody("item[item_type]", extras.getString("item_type")); 
      builder.addTextBody("item[units]", extras.getString("units")); 
      builder.addTextBody("item_media[url]", extras.getString("item_url")); 
      builder.addTextBody("item_media[mime_type]", extras.getString("mime_type")); 
      builder.addTextBody("item_media[encoding]", extras.getString("encoding")); 
      if (extras.containsKey("hasGeoTag") && extras.getString("hasGeoTag").contentEquals("true")) { 
       // add geotag name => value data 
       builder.addTextBody("geolocation[lon]", extras.getString("lon")); 
       builder.addTextBody("geolocation[lat]", extras.getString("lat")); 
       builder.addTextBody("geolocation[ele]", extras.getString("ele")); 
      } 

      httpPost.setEntity(builder.build()); 
      HttpResponse response = client.execute(httpPost); 
      HttpEntity entity = response.getEntity(); 
      String responseTmp = response.toString(); 

      entity.consumeContent(); 
      client.getConnectionManager().shutdown(); 

      return responseTmp; // dumb string response for code sample. Use something more fitting yourself. 
     } catch (ClientProtocolException e) { 
      Log.i (TAG, "Http Post Failed. ClientProtocolException Exception: "); 
      // Log exception 
      e.printStackTrace(); 
     } catch (IOException e) { 
      Log.i (TAG, "Http Post Failed. IOException Exception: "); 
      // Log exception 
      e.printStackTrace(); 
     } 

    } 
    return "Some error string as you see fit."; 
} 

WWW服務器接收以下列格式該請求的數據(如印刷用PHP的print_r($ _ POST,真))

Array 
(
    [user_id] => 98 
    [item] => Array 
     (
      [name] => Profile Image 
      [description] => image for user profile 
      [item_type] => 1 
      [units] => images 
     ) 

    [item_media] => Array 
     (
      [url] => null 
      [mime_type] => image/png 
      [encoding] => null 
     ) 

    [geolocation] => Array 
     (
      [lon] => -133.03752347 
      [lat] => 23.8897191 
      [ele] => 229.89999389648438 
     ) 

)