I am using retrofit for api calling.I need to send data to server from my application.The process is the server takes authentication(Header) token and the collection of data (Body) as parameter.I have coded the following part but it does not send the data to server.Please help me because i stuck on this since last 4 days.
JAVA: //This method will be used to submit all the data into server for cash
public void submitData(final Integer customerID, final String zone, final Double due, final Double receivedAmount, final Integer typeID, final String paymentDate
, final String remarks){
//Adding the base URL
String BASE_URL="api";
///Making a retrofit instance
Retrofit retrofit=null;
if (retrofit == null){
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit=new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
CollectionData collectionData=new CollectionData(customerID,zone,due,receivedAmount,paymentDate,typeID,remarks);
apiService=retrofit.create(AllApiService.class);
final Call<CollectionData> userInformationCall=apiService.storeData("Bearer "+tokenSTR,collectionData);
Log.v("URL",""+BASE_URL);
Log.v("TOKEN ",""+tokenSTR);
//handling user requests and their interactions with the application.
userInformationCall.enqueue(new Callback<CollectionData>() {
@Override
public void onResponse(Call<CollectionData> call, Response<CollectionData> response) {
try{
Toast.makeText(context,"Successful",Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Log.v("EXCEPTION : ",""+e.getMessage());
Toast.makeText(context,"Slow server response",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<CollectionData> call, Throwable t) {
Toast.makeText(context,"Unsuccessful",Toast.LENGTH_SHORT).show();
Log.v("ERROR : ",""+t.getMessage());
}
});
}
The server takes the data following way:
POST server_address HTTP/1.1
Host: host_name
Authorization: Bearer (token)
cache-control: no-cache
Postman-Token: 9a272ec4-87a3-4174-b5c5-a87ca478bce8
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="customer_id"
7
Content-Disposition: form-data; name="customer_zone"
Yellow
Content-Disposition: form-data; name="due"
0
Content-Disposition: form-data; name="received_amount"
0
Content-Disposition: form-data; name="collection_date"
26-01-2019
Content-Disposition: form-data; name="payment_type_id"
1
Content-Disposition: form-data; name="remarks"
Remarks
------WebKitFormBoundary7MA4YWxkTrZu0gW--