user5550554
user5550554

Reputation:

Files from Android Retrofit to ASP.net

I'm currently trying to upload Android logs (.zip) to an ASP.net server. For now I have the currently Retrofit part:

    @Headers("Accept: Application/JSON")
    @Multipart
    @POST("/api/Device/GetLog")
    Call<Result> GetLog(@Part("zippedLog")RequestBody file, 
@Part("imei") String imei, 
@Part("dateLogStart") String dateLogStart, 
@Part("dateLogEnd") String dateLogEnd);

The call is being done by the Logger Class.

         File file = new File(path);

        RequestBody requestBody =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);

        Call<Result> call = ApiServiceImpl.getInstance().GetLog(requestBody,SystemProp.getInstance().getImei(), start.toString().replace(" ", "-"), 
end.toString().replace(" ", "-"));
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(retrofit.Response<Result> response, Retrofit retrofit) {
                Result result = response.body();
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e("Upload", t.getMessage());
            }
        });

However I have no clue to how should I write the server part to wait this file. What I have tried is

[HttpPost]
[AllowAnonymous]
public DtoResultBase GetLog(byte[] zippedLog, string imei, string dateLogStart, string dateLogEnd)
{
    return Resolve(() =>
    {
        //Call Method that save the file

        return new DtoResultBase();
    });
}

What am I doing wrong?

Upvotes: 0

Views: 1694

Answers (1)

user5550554
user5550554

Reputation:

The way that I fixed this is creating an object at Server Side:

public class DtoLog
    {

        public string Imei { get; set; }
        public DateTime DateStart { get; set; }
        public DateTime DateEnd { get; set; }
        public byte[] LogStringBaseData { get; set; }
        public byte[] LogData { get; set; }
    }

And then use this obj as a parameter in the controller

[HttpPost]
        [AllowAnonymous]
        public DtoResultBase GetLog(DtoLog zippedLog)
        {
            return Resolve(() =>
            {

                // just to check data
                string imei = zippedLog.Imei;
                DateTime start = zippedLog.DateStart;
                DateTime end = zippedLog.DateEnd;
                byte[] data = zippedLog.LogData; 
                          // System.IO.File.ReadAllBytes(pathToSearch);

                return new DtoResultBase();
            });
        }

at the client side with retrofit I also created an obj Log:

public class LogZipped {

    @SerializedName("imei")
    private String imei;

    @SerializedName("dateStart")
    private String dataStart;

    @SerializedName("dateEnd")
    private String dataEnd;

    @SerializedName("logData")
    private String logData;

    public LogZipped(String imei, String dataStart, String dataEnd, String logData) {
        this.imei = imei;
        this.dataStart = dataStart;
        this.dataEnd = dataEnd;
        this.logData = logData;
    }

    public void setImei(String imei) {
        this.imei = imei;
    }

    public void setDataStart(String dataStart) {
        this.dataStart = dataStart;
    }

    public void setDataEnd(String dataEnd) {
        this.dataEnd = dataEnd;
    }

    public void setLogData(String logData) {
        this.logData = logData;
    }
}

and use this object at Retrofit's call

String data = Base64.encodeToString(bytes, Base64.DEFAULT);

        LogZipped logZipped = new LogZipped (data,
                SystemProp.getInstance().getImei(),
                start.toString().replace(" ", "-"),
                end.toString().replace(" ", "-"));

        Call<Result> call = ApiServiceImpl.getInstance().GetLog(logZipped );

Upvotes: 1

Related Questions