2015-06-29 34 views
0

我正嘗試使用camfind進行Android應用程序,並對所有請求進行改進。我發現使用多部分表單數據將圖像文件上傳到服務器時遇到了很多困難。你能解決這個問題嗎?無法管理以多部分形式發佈圖像

我的代碼:

public class getCamFind { 
//Set constants 
private static final String TAG = MainActivity.class.getSimpleName(); 
private static final String API_URL = "https://camfind.p.mashape.com"; 

//creates object getCamFind, sends request and gets answer 
static void getCamResult(String path) throws IOException { 

    File imageFile = new File(path); //THIS FILE EXISTS AND SD IS MOUNTED 


    String language = new String("en_US"); 
    String keyMashape = "XXX"; 


    RestAdapter restAdapter = new RestAdapter.Builder() 
      .setEndpoint(API_URL) 
      .setLogLevel(RestAdapter.LogLevel.FULL) 
      //.setRequestInterceptor(new SessionRequestInterceptor()) 
      .build(); 



    // Create an instance of our API interface. 
    CamFind camFind = restAdapter.create(CamFind.class); 

    camFind.send(keyMashape, language, new TypedFile("image/jpeg", imageFile), new Callback<resultClass>() { 
     @Override 
     public void success(resultClass result, Response response) { 
      Log.d(TAG, response.toString()); 
      Log.d(TAG, result.getStatus()); 
      Log.d(TAG, response.getBody().toString()); 
     } 

     @Override 
     public void failure(RetrofitError retrofitError) { 
      Log.d(TAG, "Error : " + retrofitError.getMessage()); 
     } 
    }); 
} 

static class Contributor { 
    String login; 
    int contributions; 
} 


//interface of the object getCamFind 
interface CamFind { 

    @Multipart 
    @POST("/image_requests") 

    void send(
      @Header("X-Mashape-Key") String keyMashape, 
      @Part("image_request[locale]") String language, 
      @Part("image_request[image]") TypedFile imageFile, 

      //@Field("image_request[remote_image_url]") String URL, 
      Callback<resultClass> callback); 

} 

private class resultClass{ 
    String name; 
    String status; 

    public String getName(){return this.name;} 
    public String getStatus(){return this.status;} 


} 


} 

當我這樣說,它和"{"error": {"image": ["invalid image format"]}}"當我使用的文件,而不是鍵入文件發給,它說"image": ["can't be blank"]發送。

這可能是有用的,它是用cloudsight帖子的改造日誌(基本上完全相同的東西使用mashape,請求是相同的):

 06-29 16:59:43.866 2483-2502/bookshotco.bookshot2 D/Retrofit﹕ ---> HTTP POST https://api.cloudsightapi.com/image_requests 
    06-29 16:59:43.866 2483-2502/bookshotco.bookshot2 D/Retrofit﹕ Authorization: CloudSight XXX 
    06-29 16:59:43.866 2483-2502/bookshotco.bookshot2 D/Retrofit﹕ Content-Type: multipart/form-data; boundary=ac3731a5-466f-4baf-87d1-b21fddc41701 
    06-29 16:59:43.866 2483-2502/bookshotco.bookshot2 D/Retrofit﹕ Content-Length: 531 
    06-29 16:59:43.879 2483-2502/bookshotco.bookshot2 D/Retrofit﹕ --ac3731a5-466f-4baf-87d1-b21fddc41701 
    Content-Disposition: form-data; name="image_request[locale]" 
    Content-Type: text/plain; charset=UTF-8 
    Content-Length: 5 
    Content-Transfer-Encoding: binary 
    en_US 
    --ac3731a5-466f-4baf-87d1-b21fddc41701 
    Content-Disposition: form-data; name="image_request[image]"; filename="JPEG_20150627_231626_1086927409.jpg" 
    Content-Type: image/jpeg 
    Content-Length: 0 
    Content-Transfer-Encoding: binary 
    --ac3731a5-466f-4baf-87d1-b21fddc41701-- 
    06-29 16:59:43.880 2483-2502/bookshotco.bookshot2 D/Retrofit﹕ ---> END HTTP (531-byte body) 

這也可能是有用的(看看「image_requests 「部分):http://cloudsight.readme.io/v1.0/docs/testinput

回答

1

正如您在documentation of retrofit中看到的,構造函數的第一個參數不是文件的路徑。

TypedFile(String mimeType, File file) 
Constructs a new typed file. 

這是mimetype。嘗試

new TypedFile("image/png", imageFile) 

new TypedFile("image/jpeg", imageFile) 
+0

好吧,這是一個愚蠢的錯誤,感謝您的!嘗試再次,它仍然無法正常工作,雖然(相同的確切錯誤) – Dominus

+0

你可以嘗試「application/octet-stream」作爲mimetype? –

+0

同樣的錯誤:( – Dominus