MultiPart Body First Android Retrofit

private void sendCartItems() { Number = new Random(); for(int i =0;i<200;i++){ Rnumber = Number.nextInt(900)+i; finall = finall+Rnumber; Log.i("ok",String.valueOf(finall)); } // Log.i("okk",String.valueOf(finall)); String id = "BTUKID14"; String ids = id.concat(String.valueOf(finall)); Log.i("okk2",String.valueOf(ids)); cart = new Database(this).getCarts(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Utils.SendOrderDataUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); RetrofitOrderApi api = retrofit.create(RetrofitOrderApi.class); for (int i=0; i<cart.size(); i++) { String prod_name = cart.get(i).getProductName(); String order_mail = "sth"; String prod_price = cart.get(i).getTotalPrice(); String prod_id = cart.get(i).getProductId(); String qty = cart.get(i).getQuintalNo(); String transactId = ids; String userID = user_mailId; RequestBody mFile = RequestBody.create(MediaType.parse("image/*"), cart.get(i).getImage()); MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("product_img",cart.get(i).getImage(), mFile); Call<ResponseBody> call = api.sendCartData(userID,prod_id,prod_name,cart.get(i).getPrice(), cart.get(i).getTotalPrice(),cart.get(i).getQuintalNo(),transactId,fileToUpload); Log.i("ok",prod_id.toString()+"\t"+prod_name.toString()+"\n"+cart.get(i).getPrice().toString()+"\n"+cart.get(i).getQuintalNo().toString()+"\n"+userID+"\n"+transactId+"\n"+cart.get(i).getTotalPrice()); //finally performing the call call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if (internetConnectionCheck.verifyInternetConn()) { if (response.isSuccessful()) { Toast.makeText(getApplicationContext(), "Your Order Place Successfully...", Toast.LENGTH_LONG).show(); Intent intent = new Intent(CartActivity.this, ThankYouActivity.class); startActivity(intent); finish(); Log.i("ok", "Your Order Place Successfully..."); wipeData(); pd.hide(); } else { pd.hide(); Log.i("ok", "Error! Order not placed "); Toast.makeText(getApplicationContext(), "Some error occurred...", Toast.LENGTH_LONG).show(); } }else { pd.hide(); Toast toast = Toast.makeText(CartActivity.this, R.string.internet_msg, Toast.LENGTH_LONG); View view = toast.getView(); view.setBackgroundResource(R.drawable.backgroundcolor); TextView toastText = (TextView) view.findViewById(android.R.id.message); toastText.setTextColor(Color.parseColor("#FF0000")); toast.show(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show(); pd.hide(); } }); } // @POST("cart_order") @Multipart Call<ResponseBody> sendCartData(@Part("order_mail") String mail, @Part("product_id") String productId, @Part("product_name") String productName, @Part("product_price") String productPrice, @Part("total_price") String totalPrice, @Part("quintal_no") String quintal_no, @Part("transact_id") String transactId, @Part MultipartBody.Part file);
Its about how to send the multi data to the server from android

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.