I have an image upload web service in node.js , it runs well , I can upload images by postman , and I try to take photo in android than upload it , node.js always reply
'undefined'
no error message , only the server show undefined
I use the same code as postman give me , but can't work , help please , stuck at upload for two days
my code
imageUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Runnable r = new Runnable() {
@Override
public void run() {
final String boundary;
boundary = "===" + System.currentTimeMillis() + "===";
OkHttpClient client = new OkHttpClient();
File file = new File(imageLocation);
String fileName = file.getName();
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=" + boundary);
RequestBody body = RequestBody.create(mediaType, boundary + "Content-Disposition: form-data; name=\\\"images\"; filename="+fileName+"\\\"\\r\\nContent-Type: image/jpeg\\\"\\r\\n\\r\\n\\r\\n" + boundary);
Request request = new Request.Builder()
.url("http://localhost:3000/upload")
.post(body)
.addHeader("content-type", "multipart/form-data; boundary="+boundary)
.addHeader("cache-control", "no-cache")
.build();
try {
Response response = client.newCall(request).execute();
}catch (IOException e){
e.printStackTrace();
}
}
};
Postman code
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=-- -011000010111000001101001");
RequestBody body = RequestBody.create(mediaType, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"images\"; filename=\"Costa Rican Frog.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n-----011000010111000001101001--");
Request request = new Request.Builder()
.url("http://localhost:3000/upload")
.post(body)
.addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "e360f8ea-d179-fc78-6dff-c56791b7d9ba")
.build();
Response response = client.newCall(request).execute();