2017-10-19 81 views
0

我使用的是API這是在我的掌握,以及已經加入了一個開發團隊最近正在使用Retrofit1改造1,發送帶有自定義按鍵多形式的數據值

我無法將請求發送到需要的格式服務器,因爲服務器需要多形式的數據體的格式如下:

Uniqueidentifier:FileName.jpg:ReroutServerIP:Base64EncodedDocString. 

我已經爲了做到這一點嘗試了許多不同的技術任務,但我找不到任何工作方法來做到這一點。服務器告訴我消息格式不受支持。這是我目前的代碼(去掉了網址)。請有人協助?

@POST("URL") 
public Response post_SendData(@Header("Content-Type") String ContentType, @Body String body); 

爲了達到理想的效果,我可以使用沒有標題的郵遞員,並使用form-data post方法從我的系統發佈文件。在工作郵差帖子中,密鑰是上面提到的格式化字符串,值是從我的桌面上選擇的文件。請看下面的郵遞員(編輯刪除網址)。

Postman

非常感謝球員。

+0

你要一個多文件發送到服務器嗎? –

+0

的確,但他們有一個非常奇怪的格式,我必須遵守。 –

+0

我編輯了這篇文章,包括我已經能夠得到這個工作的唯一方法(使用郵差)。 –

回答

0

如果你想將文件發送到服務器:

public void uploadPictureRetrofit(File file, Callback<YourObject> response) { 


     // this will build full path of API url where we want to send data. 
     RestAdapter restAdapter = new RestAdapter 
       .Builder() 
       .setEndpoint(YOUR_BASE_URL) 
       .setConverter(new SimpleXMLConverter()) // if the response is an xml 
       .setLogLevel(RestAdapter.LogLevel.FULL) 
       .build(); 

     // SubmitAPI is name of our interface which will send data to server. 
     SendMediaApiInterface api = restAdapter. 
       create(SendMediaApiInterface.class); 
     TypedFile typedFile = new TypedFile(MULTIPART_FORM_DATA, file); 

     api.sendImage(typedFile, response); 
    } 

這是接口SendMediaApiInterface:

public interface SendMediaApiInterface { 
    @Multipart 
    @POST("url") 
    void sendImage(@Part("here_is_the_attribute_name_in_webservice") TypedFile attachments, Callback<YourObject> response);} 
+0

我認爲這與我所需要的很接近,唯一的問題是每次都需要指定@Part名稱,因爲唯一標識符對用戶是唯一的。你知道我能做到這一點嗎? –

+0

例如,如果** ** here_is_the_attribute_name_in_webservice = ** 「image_attribute」 ** 你的API網址將是這樣的:http://base_ur_example.com/upload_photo_example?image_attribute= 請檢查您的API,並填寫相應的attribut –

+0

我編輯過這個帖子,也許我問的是錯誤的問題? –