2

I'm using RoboSpice Google Http Java Client module and I'm successfully executing POST request using JSON data.

Problem : My problem is now I've to attach byte[] data in POST request but I'm unable to find any way to do this with RoboSpice Google Http Java Client module.

Here is my code snippet :

public class SaveUserProfile_Request extends GoogleHttpClientSpiceRequest<SaveUserProfile> {
    private String apiUrl;
    private JSONObject mJsonObject;
    private String filePath;

    public SaveUserProfile_Request(JSONObject mJsonObject, String filePath) {
    super(SaveUserProfile.class);
    this.apiUrl = AppConstants.GLOBAL_API_BASE_ADDRESS + AppConstants.API_SAVE_PROFILE;
    this.mJsonObject = mJsonObject;
    this.filePath = filePath;
    }

    @Override
    public SaveUserProfile loadDataFromNetwork() throws IOException {
    Ln.d("Call web service " + apiUrl);

    HttpRequest request = getHttpRequestFactory().buildPostRequest(new GenericUrl(apiUrl),
        ByteArrayContent.fromString("application/json", mJsonObject.toString()));

    request.setParser(new JacksonFactory().createJsonObjectParser());

    return request.execute().parseAs(getResultType());
    }

}

I tried using Multipart HTTP Requests but it doesn't work.

Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76

1 Answers1

0

check this answer here https://stackoverflow.com/a/17151284/881771 You can add the FileUpload logic to your loadFromNetwork method before calling

request.execute().parseAs(getResultType());
Community
  • 1
  • 1
Gaurav
  • 840
  • 4
  • 16