0

Not too sure on how to word the title but is there an easier way of POSTing data to a WCF Service using Retrofit? Currently I am using the @Body attribute and something like this. Service expects json:

This is what I want to send across

public class Photo
{
    private String imageData;
    private String latitude;
    private String longitude;

    public Photo(String imageData, String latitude, String longitude)
    {
        this.imageData = imageData;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String getImageData()
    {
        return imageData;
    }

    public void setImageData(String imageData)
    {
        this.imageData = imageData;
    }

    public String getLatitude()
    {
        return latitude;
    }

    public void setLatitude(String latitude)
    {
        this.latitude = latitude;
    }

    public String getLongitude()
    {
        return longitude;
    }

    public void setLongitude(String longitude)
    {
        this.longitude = longitude;
    }
}

With this:

@POST("/PhotoService.svc/addPhoto")
    void addPhoto(@Body Photo photo, Callback<String> cb);

And then I am calling it like this:

IPhotoService photoService = restAdapter.create(IPhotoService.class);
            photoService .addPhoto(new Photo(photoSerialised, String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude())), new Callback<String>()

This all works fine, but I have multiple Service Endpoints with various parameters. Do I need to create a Model for each 'Body' or is there a simpler way. Similar to doing a GET?

I have tried using @Field where I have multiple parameters, but on my WCF side my parameters come through as null. Simply sending a class with the @Body attribute works fine, but I do not want to create a model class for each available Service endpoint, or is there no other way?

pjdupreez
  • 659
  • 1
  • 8
  • 19

1 Answers1

1

There is two way to put parameters with POST request

First, you can use the @FieldMap, check out the link Retrofit Javadoc, the service must use with @FormUrlEncoded annotation

Second, you actually can use MultipartTypedOutput to put parameters into @Body Example of putting parameter into body, but MultipartTypedOutput no longer exists in Retrofit 2.0.0-beta1

So if you wants to upload image together with other parameter, maybe you can do like this

@POST("/PhotoService.svc/addPhoto")
void addPhoto(@Body Photo photo,@FieldMap Map<String, String> fields, Callback<String> cb);

//Example
Map<String,String> fields = new HashMap<String,String>();
fields.put("params1","value of params");
fields.put("params2","value of params 2");

Photo photo = new Photo(photoSerialised, String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()));

photoService .addPhoto(photo,fields,new Callback<String>()

updates

By the way you actually can use Multipart request with @Part or @PartMap, please find it in Retrofit javadoc, there is an example

Community
  • 1
  • 1
Louis Loo
  • 641
  • 1
  • 7
  • 11
  • I'm getting HTTP 500 errors from my web service, yet testing it through Fiddler works fine. Any idea on what I need to check? – pjdupreez Sep 13 '15 at 10:22
  • Is it your server up and running? – Louis Loo Sep 13 '15 at 12:32
  • Why yes, yes it is. I think WCF is getting the data but does not know what to do with it. how can I check what is received by WCF? – pjdupreez Sep 13 '15 at 19:09
  • Maybe u have to check with ur server side, because it shown Internal Server Error which is status code 500. You can log down the data which passed to server, im not familiar with c# so i cant gv u an example, but in PHP is something like this – Louis Loo Sep 13 '15 at 20:34