Hi All,
I solve this problem and has nothing to do with wp rest api,
with the beta-2.8 just release I can now upload image to the server
using the media endpoints…
Below is the code I used with okHttp which you can compile via
gradle >>> compile ‘com.squareup.okhttp:okhttp:2.6.0’
OkHttpClient client = new OkHttpClient();
MediaType MEDIA_TYPE_JPG = MediaType.parse(“image/jpg”);
File sourceFile = new File(filepath);
Uri uri = new Uri.Builder()
.scheme(protocol)
.authority(domain)
.path(“wp-json/wp/v2/media”)
.appendQueryParameter(“access_token”, accessToken)
.build();
String serverUri = String.valueOf(uri);
String[] image = filepath.split(“/”);
int idx = image .length – 1;
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addFormDataPart(“file”, image [idx], RequestBody.create(MEDIA_TYPE_JPG, sourceFile))
.build();
Request request = new Request.Builder()
.url(serverUri)
.post(requestBody)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
Hope this helps…