I have made a RESTful web service which I am currently hosting on Google App Engine. The web service works well when I test it using Postman, as it provides the appropriate JSON response. When using Postman, I do not provide any authorization (image 1) and I do include an image in the body as 'binary' (image 2).
However, I am not sure how to go about sending an equivalent request in Java for an Android application. How do I go about forming such a request and including the binary version of an image within the request?
Any help would be much appreciated!
Thanks in advance!
Below is the code I currently have, but I do not get a response from my web-service, so I assume that I have done something incorrectly in my code:
import android.app.DownloadManager;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by OliverM on 17/12/2017.
*/
public class Downloader extends AsyncTask<File, Integer, String> {
protected void onPreExecute(){
}
protected String doInBackground(File... files) {
OkHttpClient client = new OkHttpClient();
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image", files[0].getName(), RequestBody.create(MediaType.parse("image/jpeg"), files[0]))
.build();
Request request = new Request.Builder()
.url("https://----------------.appspot.com/classify")
.post(body)
.addHeader("Cache-Control", "no-cache")
.build();
try {
Response response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(String result) {
}
}