4

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();
Awei Hsue
  • 247
  • 5
  • 17
  • 1
    you can check out this url https://github.com/square/okhttp/tree/master/samples/guide/src/main/java/okhttp3/recipes for different examples on how to post forms, add images and so on. – legrandviking Mar 03 '16 at 15:50
  • Here is a link to how I did it: http://stackoverflow.com/a/37942387/447549 – Clive Jefferies Jun 21 '16 at 10:52

0 Answers0