1

I'm using Retrofit in my Android app to do a couple of HTTP requests. The first is to get all the information in a specific table (this is working and the fields are being deserialized properly). The second is to insert a new row into that same table. When inserting into the table, all my data appears blank in the DB (my strings are "" and my integers are 0s). When I insert manually using Postman, everything inserts fine. I have inspected the object that I'm sending in the request and all the data is populated correctly.

Here is my interface:

public interface FormRequests {
    @POST("/createForm.php")
    public Call<Response> CreateForm(@Body Form form);

    @POST ("/getAllForms.php")
    public Call<ArrayList<Form>> GetAllForms();
}

Here is where I'm trying to insert the form:

Retrofit retrofit = new Retrofit.Builder().baseUrl(DatabaseConstants.URL).addConverterFactory(GsonConverterFactory.create()).build();
FormRequests service = retrofit.create(FormRequests.class);
final Call<Response> call = service.CreateForm(form);
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            retrofit.Response<Response> response = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();

Here is the JSON from the insert when I pull all the table data:

[{"id":"5","date":"","miles":"0","rate":"0","ticket_number_1":"0","quantity_1":"0","lease_number_1":"0","amount_1":"0","ticket_number_2":"0","quantity_2":"0","lease_number_2":"0","amount_2":"0","total_quantity":"0","fuel_percent":"0","fuel_price":"0","ip_minutes":"0","ip_charge":"0","lp_minutes":"0","lp_charge":"0","other_fees_1":"0","other_fees_reason_1":"","other_fees_2":"0","other_fees_reason_2":"","remarks":"","ip_wait_reason":"","lp_wait_reason":"","company":"","driver":"","truck":"","trailer":""}]

EDIT: Here's some more information of what my server is seeing. I logged the SQL command when I use Postman and then my app, respectively:

INSERT INTO `database`.`forms` (`date`, `miles`, `rate`, `ticket_number_1`, `quantity_1`, `lease_number_1`, `amount_1`, `ticket_number_2`, `quantity_2`, `lease_number_2`, `amount_2`, `total_quantity`, `fuel_percent`, `fuel_price`, `ip_minutes`, `ip_charge`, `lp_minutes`, `lp_charge`, `other_fees_1`, `other_fees_reason_1`, `other_fees_2`, `other_fees_reason_2`, `remarks`, `ip_wait_reason`, `lp_wait_reason`, `company`, `driver`, `truck`, `trailer`) VALUES ('11/13/2015', '500', '1.16', '2342424', '12.50', '211422', '10.50', '90897','29.50', '249023', '49.20', '23483.60', '24.12', '2023.23', '23', '23.3', '39', '29.5', '65.3', 'Tank Refusal', '98.90', 'Rough Road', 'This is my remark and it is awesome', 'The tank was refused', 'The tank was not refused', 'Windy Truck LLC', 'Al Johnson', '227 ROF', 'T02');

INSERT INTO `database`.`forms` (`date`, `miles`, `rate`, `ticket_number_1`, `quantity_1`, `lease_number_1`, `amount_1`, `ticket_number_2`, `quantity_2`, `lease_number_2`, `amount_2`, `total_quantity`, `fuel_percent`, `fuel_price`, `ip_minutes`, `ip_charge`, `lp_minutes`, `lp_charge`, `other_fees_1`, `other_fees_reason_1`, `other_fees_2`, `other_fees_reason_2`, `remarks`, `ip_wait_reason`, `lp_wait_reason`, `company`, `driver`, `truck`, `trailer`) VALUES ('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');

I also just ran my form through GSON, to see if it can be converted into JSON, and this is what I get:

{"amount_1":"15.16","amount_2":"12.33","company":"Windy Truck LLC","date":"11/14/2015","driver":"Al Johnson","fuel_percent":"23","fuel_price":"234.00","ip_charge":"245.00","ip_minutes":"270","ip_wait_reason":"This is a wait reason","lease_number_1":"100","lease_number_2":"1234123","lp_charge":"332.50","lp_minutes":"345","lp_wait_reason":"This is another wait reason","miles":"200","other_fees_1":"343.00","other_fees_2":"233.00","other_fees_reason_1":"Rough Road","other_fees_reason_2":"Split Load","quantity_1":"65.16","quantity_2":"890.16","rate":"5.53","remarks":"This is a remark","ticket_number_1":"50","ticket_number_2":"16549","total_quantity":"2,342.64","trailer":"03","truck":"227 ROF"}

Everything there looks fine, but it looks like Retrofit isn't creating the right GSON object, but I'm not sure if that can happen.

I've searched Google/stackoverflow, but can't find anything pertaining to this. Any suggestions would be great because I don't know how to troubleshoot Retrofit very well.

jward
  • 23
  • 5
  • that probably has more to do with your backend – njzk2 Nov 14 '15 at 05:41
  • @njzk2 Even though I'm able to use my backend to add a form using Postman? – jward Nov 14 '15 at 18:42
  • can you elaborate what do you mean by insert using postman. Also your returned json appears to be a list. can you post your code of how you are handling that list before you are inserting it in your db. – harshitpthk Nov 14 '15 at 20:34
  • retrofit has powerful logging options to see what is actually sent. try them – njzk2 Nov 14 '15 at 22:08
  • @hars I'm not sure which JSON you're referring to. Before my server inserts into my db, it creates the SQL command that I posted above, using my URL parameters, and executes it. The first SQL command is what my server sees when I send the POST request using Postman. The second one is what my server sees when I send using Retrofit from my app. – jward Nov 15 '15 at 00:46
  • @njzk2 Retrofit 2 doesn't have any logging features like the first one did. – jward Nov 15 '15 at 00:46
  • for logging in retrofit 2 : http://stackoverflow.com/questions/32514410/logging-with-retrofit-2 – njzk2 Nov 15 '15 at 01:26
  • @njzk2 Thanks for that. I just logged my request and it looks fine, so I need to think more why my server isn't getting the information properly. – jward Nov 15 '15 at 01:28
  • compare your request with the request for postman. A proxy could help you pinpoint what the differences are. possibly the content-type header? – njzk2 Nov 15 '15 at 01:29
  • @njzk2 Thanks so much for the help! It turns out Postman was sending using MIME and Retrofit was using JSON. My server wasn't set up to decode JSON properly. You're initial comment blaming my server was right. Sorry it took me awhile to realize it :P – jward Nov 15 '15 at 17:09
  • I am facing the same issue but sometimes that too for some users. Related logs : 2017-04-16 12:44:59,414 - middlewares.logging_middleware - INFO - Logging Api Request - {'url': /login/', 'log_sub_type': 'POST', 'log_type': 'api', 'request_params': {'username': 'patrick.d', 'password': '**********'}}. Any suggestions would be great. 2017-04-16 14:52:52,943 - middlewares.logging_middleware - INFO - Logging Api Request - {'url': /login/', 'log_sub_type': 'POST', 'log_type': 'api', 'request_params': {}} – Sakshi Gupta Apr 17 '17 at 10:08

0 Answers0