Retrofit File Upload:
Config class:
public class RetroConfig {public static Retrofit retrofit(){
OkHttpClient client = new OkHttpClient();
client.retryOnConnectionFailure();
client.cache();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://bestvote.in/privilege/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
Create Api Interface:
import com.squareup.okhttp.RequestBody;import com.squareup.okhttp.Response;
import org.json.JSONObject;
import okhttp3.ResponseBody;
import retrofit.Call;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.Part;
import retrofit.http.Path;
import retrofit.http.Query;
public interface ApiInterface {
@Multipart
@POST("insertpost.php")
Call<CommonResponse> submitForm(@Part("description") RequestBody desc, @Part("audio\"; filename=\".mp3\" ") RequestBody audio,
@Part("image\"; filename=\".jpg\" ") RequestBody photo,
@Part("late") RequestBody lat, @Part("long") RequestBody lon);
@GET("allpost.php")
Call<SellerResponse> getSellerAds();
}
Call Api Post method:
import retrofit.Call;import retrofit.Callback;
import retrofit.Retrofit;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
private void submitForm() {
String desc = details.getText().toString();
if (!desc.equals("") && imagePath!=null && audioPath!=null) {
RequestBody descBody = RequestBody.create(MediaType.parse("text/plain"), desc);
RequestBody latBody = RequestBody.create(MediaType.parse("text/plain"), latFinal);
RequestBody lonBody = RequestBody.create(MediaType.parse("text/plain"), lngFinal);
RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), new File(imagePath));
RequestBody audioBody = RequestBody.create(MediaType.parse("multipart/form-data"), new File(audioPath));
//RequestBody videoBody = RequestBody.create(MediaType.parse("multipart/form-data"), new File(videoPath));
final ProgressDialog dialog = new ProgressDialog(SellerActivity.this);
dialog.setMessage("Uploading...");
dialog.setCancelable(false);
dialog.show();
Call<CommonResponse> call = RetroConfig.client().submitForm(descBody, audioBody, imageBody, latBody, lonBody);
call.enqueue(new Callback<CommonResponse>() {
@Override
public void onResponse(retrofit.Response<CommonResponse> response, Retrofit retrofit) {
dialog.cancel();
if (response.body() != null) {
CommonResponse response1 = response.body();
if(response1.getResponse().equals("success")){
Toast.makeText(SellerActivity.this, "Upload Ads successfully..", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SellerActivity.this, SellerAds.class));
finish();
}
}
}
@Override
public void onFailure(Throwable t) {
Log.e("submitError", t.getLocalizedMessage());
dialog.cancel();
}
});
} else {
Utils.showDialog(SellerActivity.this, "Please select All files");
}
}
CallGet Method:
RetroConfig.client().getSellerAds().enqueue(new Callback<SellerResponse>() {@Override
public void onResponse(Response<SellerResponse> response, Retrofit retrofit) {
SellerResponse sr = response.body();
String status = sr.getResponse();
if(status.equals("success")){
List<SAds> sAdses = sr.getSellerAds();
rv.setAdapter(new SellerAdsAdapter(SellerAds.this, sAdses));
}
}
@Override
public void onFailure(Throwable t) {
Log.e("error", t.getLocalizedMessage());
}
});