0

I'm writing an android app that needs to communicate with a server. I'm struggling to make a MultipartBody for OkHttp3 to use. My code looks like this:

OkHttpClient client = new OkHttpClient();

try 
{
    RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("someKey1", "someString1")
                    .addFormDataPart("someKey2", "someString2")
                    .build();
    System.out.println(requestBody.toString());

    Request request = new Request.Builder()
                    .url("server url")
                    .post(requestBody)
                    .build();

    Response response = client.newCall(request).execute();
    return response;
}
catch (IOException e)
{
    e.printStackTrace();
    return false;
}

This is pretty much the standard code everyone uses. The server receives the request, but I can't get the MultipartBody from the request. When the multipart body is printed, it looks like this:

I/System.out: okhttp3.MultipartBody@14040826

So my questions are: Why doesn't it contain my data and should it? Am I missing a library (I included compile 'com.squareup.okhttp3:okhttp:3.2.0' in the gradle and assume MultipartBody is part of this library)? What is a MultipartBody supposed to look like?

I tried sending a FormBody as well but it looks the same.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
hgb
  • 1
  • 1
  • 2
  • See your answer here: http://stackoverflow.com/questions/24279563/uploading-a-large-file-in-multipart-using-okhttp – Tosin Onikute Mar 31 '16 at 11:14
  • Thank you, I did look at those examples. I am however more concerned with what the MultipartBody should look like when converted to string to ensure that I'm sending the correct data. – hgb Mar 31 '16 at 11:48
  • Wait, aren't you using multipart ONLY because you need to send files? – Tosin Onikute Mar 31 '16 at 12:00
  • You are right, sorry I should have posted it with a FormBody. I did manage to get it working though, requestBody.toString() only returns the name. Thanx – hgb Apr 01 '16 at 08:06

1 Answers1

0

Remove okhttp3.Response response = client.newCall(request).execute();

    okhttp3.Call call = client.newCall(request);
    call.enqueue(new okhttp3.Callback() {
        @Override
        public void onFailure(okhttp3.Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {

        }

    });
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182