Reputation: 19
I want to upload two videos and some text fields into the retrofit library using Multipart Post method, How to Send Value using the android retrofit library
API Interface
@Headers({"Accept: application/json"})
@Multipart
@POST("event")
Call<ResponsePojo> submitData(@Part MultipartBody.Part video,
@Part("device_id") String device_id,
@Part("lat") String lat,
@Part("lng") String lng,
@Part("speed") String speed,
@Part("event_type") String event_type,
@Part MultipartBody.Part videolarge);
ResponsePoja model Class
public class ResponsePojo {
@SerializedName("fileData")
@Expose
private String fileData;
@SerializedName("device_id")
@Expose
private String device_id;
@SerializedName("lat")
@Expose
private String lat;
@SerializedName("lng")
@Expose
private String lng;
@SerializedName("speed")
@Expose
private String speed;
@SerializedName("event_type")
@Expose
private String event_type;
public ResponsePojo(String fileData, String device_id, String lat, String lng, String speed, String event_type) {
this.fileData = fileData;
this.device_id = device_id;
this.lat = lat;
this.lng = lng;
this.speed = speed;
this.event_type = event_type;
}
public String getFileDatasmall() {
return fileData;
}
public void setFileDatasmall(String fileDatasmall) {
this.fileData = fileDatasmall;
}
public String getDevice_id() {
return device_id;
}
public void setDevice_id(String device_id) {
this.device_id = device_id;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
public String getSpeed() {
return speed;
}
public void setSpeed(String speed) {
this.speed = speed;
}
public String getEvent_type() {
return event_type;
}
public void setEvent_type(String event_type) {
this.event_type = event_type;
}
Bellow Send Button Click Method ,When i click time upload all data save to server
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100,TimeUnit.SECONDS).build();
Retrofit builder = new Retrofit.Builder()
.baseUrl(API.BaseUrl).client(client)
.addConverterFactory(GsonConverterFactory.create(new Gson())).build();
API api = builder.create(API.class);
//create file which we want to send to server.
File videoFIle = new File(String.valueOf(realUri));
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), videoFIle);
MultipartBody.Part image = MultipartBody.Part.createFormData("fileData", videoFIle.getName(), requestBody);
Call<ResponsePojo> call = api.submitData(image, "1, ", "4.667566", "54.54448", "5457", "2",image);
call.enqueue(new Callback<ResponsePojo>() {
@Override
public void onResponse(Call<ResponsePojo> call, Response<ResponsePojo> response) {
ResponsePojo body = response.body();
Toast.makeText(getApplicationContext(), String.valueOf("Code "+response.message()), Toast.LENGTH_SHORT).show();
pd.dismiss();
}
@Override
public void onFailure(Call<ResponsePojo> call, Throwable t) {
Toast.makeText(getApplicationContext(), "File "+t.toString(), Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
Upvotes: 1
Views: 851
Reputation: 8853
If you are using @POST
and want to send data using @part
you need to first convert it to RequestBody
before sending it. Do the following changes
In request code
Call<ResponsePojo> submitData(@Part MultipartBody.Part video,
@Part("device_id") RequestBody device_id,...
Before calling this method you need to convert your parameter to Requestbody
RequestBody device_id = RequestBody.create(
MediaType.parse("text/plain"),
device_id);
Now use this variable as mentioned above in method call.
Upvotes: 0
Reputation: 967
For this instance,a 404 means there is no API for this URL.
Maybe, your URL needs to be http://192.168.0.105/register/
instead of http://192.168.0.105/register
or maybe it is malformed.
Example, http://192.168.0.105//register/
Upvotes: 1
Reputation: 37
This error basically related to the path(@Path) in some cases. so please check your request path just like @Path("/event").
According to the response code, the client was able to communicate with a given server, but the server could not find what was requested.
So, in this case, should check path and parameter what are we sending.
Upvotes: 0