简体   繁体   中英

Getting null response with response code 500 in retrofit 2

I'm implementing an app where I've a user profile that I want to edit so I get all the user input as strings and send them to the server using retrofit 2, I tried the code many times only 1 out of 100 try works fine and the rest I get a null response with response code 500

here you can see my code :

this is the interface

public interface ApiInterface {

    @POST("edit/profile")
    @FormUrlEncoded
    Call<AuthModel> editProfile (@Field("api_token") String api_token,
                                   @Field("profilePic") String profilePic,
                                 @Field("name") String name,
                                 @Field("email") String email,
                                 @Field("phoneNo") String phoneNo,
                                 @Field("state") String state,
                                 @Field("city") String city,
                                 @Field("age") String age,
                                 @Field("sex") String sex,
                                 @Field("lastBooking") String lastBooking,
                                 @Field("hostel") String hostel,
                                 @Field("roomNumber") String roomNumber,
                                 @Field("status") String status);
}

this is the AuthModel

public class AuthModel {

    @Expose
    @SerializedName("api_token")
    private String api_token;
    @Expose
    @SerializedName("api_token_status")
    private String api_token_status;
    @Expose
    @SerializedName("status")
    private String status;
    @Expose
    @SerializedName("message")
    private String message;


    public String getApi_token() {
        return api_token;
    }

    public String getMessage() {
        return message;
    }

    public String getApi_token_status() {
        return api_token_status;
    }

    public String getStatus() {
        return status;
    }
}

this is the ApiClient

public class ApiClient {

    public static final String BASE_URL = "http://www.kyz.com/api/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {

            Gson gson = new GsonBuilder()
                    .create();

            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }
        return retrofit;
    }
}

this is the method where I make the request

public void editProfileRequest (final Context context, String api_token, String profilePic, String name, String email, String phoneNo, String state,
                                String city, String age, String sex, String lastBooking, String hostel, String roomNumber, String status) {

    apiInterface = ApiClient.getClient().create(ApiInterface.class);
    Call<AuthModel> call = apiInterface.editProfile(api_token, profilePic, name, email, phoneNo, state, city, age, sex,
            lastBooking, hostel, roomNumber, status);
    call.enqueue(new Callback<AuthModel>() {
        @Override
        public void onResponse(Call<AuthModel> call, Response<AuthModel> response) {
            AuthModel result = response.body();
            if(result != null) {
                String status = result.getStatus();
                String msg = result.getMessage();
                if(status.equals("true")) {
                    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
                }
            } else {
                try {
                    //Toast.makeText(context, response.errorBody().string() , Toast.LENGTH_LONG).show();
                    Log.v("errorBody", response.errorBody().string());
                    Log.v("respCode", String.valueOf(response.code()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(Call<AuthModel> call, Throwable t) {
            Toast.makeText(context, t.toString(), Toast.LENGTH_LONG).show();
        }
    });
}

last thing I want to share with you is the response.errorBody()

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry &#039;barsoum@email.com&#039; for key &#039;users_email_unique&#039; (SQL: update `users` set `name` = Barsoum, `email` = barsoum@email.com, `city` = Asyut, `bloodType` = B+, `hospital` = St Louis, `roomNumber` = 555, `numOfPackets` = 6, `status` = Preparing for Surgery, `updated_at` = 2019-03-06 22:02:37 where `id` = 30) in file /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664
Stack trace:
  1. Illuminate\Database\QueryException-&gt;() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
  2. PDOException-&gt;() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:483
  3. PDOStatement-&gt;execute() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:483
  4. Illuminate\Database\Connection-&gt;Illuminate\Database\{closure}() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:657
  5. Illuminate\Database\Connection-&gt;runQueryCallback() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:624
  6. Illuminate\Database\Connection-&gt;run() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:490
  7. Illuminate\Database\Connection-&gt;affectingStatement() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:423
  8. Illuminate\Database\Connection-&gt;update() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2508
  9. Illuminate\Database\Query\Builder-&gt;update() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:780
 10. Illuminate\Database\Eloquent\Builder-&gt;update() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:693
 11. Illuminate\Database\Eloquent\Model-&gt;performUpdate() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:608
 12. Illuminate\Database\Eloquent\Model-&gt;save() /home/keesbdco/public_html/app/Http/Controllers/Api/ProfileController.php:57
 13. App\Http\Controllers\Api\ProfileController-&gt;edit() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
 14. call_user_func_array() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
 15. Illuminate\Routing\Controller-&gt;callAction() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45
 16. Illuminate\Routing\ControllerDispatcher-&gt;dispatch() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Routing/Route.php:212
 17. Illuminate\Routing\Route-&gt;runController() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Routing/Route.php:169
 18. Illuminate\Routing\Route-&gt;run() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:665
 19. Illuminate\Routing\Router-&gt;Illuminate\Routing\{closure}() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
 20. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /home/keesbdco/public_html/app/Http/Middleware/typeapi.php:19
 21. App\Http\Middleware\typeapi-&gt;handle() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 22. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 23. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /home/keesbdco/public_html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
 24. Illuminate\Routing\Middleware\SubstituteBindings-&gt;handle() /home/keesbdco/public_html/vendor/laravel/framework/src/I

please help me to solve this issue I don't think it's a serverside bug because 1% of my tries works fine and 99% I get this issue. please help, Thanks

Response code 500 is Internal Server Error , meaning something is wrong on its side.

See this link for reference on HTTP resonse status codes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM