无法管理 POST 多部分形式的图像
Can't manage to POST image in multipart-form
我正在尝试将 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"]
.
这可能会有用,它是我的 POST 使用 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
正如您在 documentation of retrofit 中看到的,构造函数的第一个参数不是文件的路径。
TypedFile(String mimeType, File file)
Constructs a new typed file.
是mimetype。尝试
new TypedFile("image/png", imageFile)
或
new TypedFile("image/jpeg", imageFile)
我正在尝试将 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"]
.
这可能会有用,它是我的 POST 使用 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
正如您在 documentation of retrofit 中看到的,构造函数的第一个参数不是文件的路径。
TypedFile(String mimeType, File file)
Constructs a new typed file.
是mimetype。尝试
new TypedFile("image/png", imageFile)
或
new TypedFile("image/jpeg", imageFile)