500 Error code with “Internal server Error” message, while doing POST method using MVVM and API in android application

143
June 01, 2021, at 05:00 AM

For API confirmation, I have tried in POSTMAN app, But response is getting properly and the data has been inserted successfully. Below is the result of POSTMAN.

As am new to android application. this is my first program using API with MVVM concepts. Here is the code which I tried. Please guide me find the issue.

// Base URL

public class ApiClient {
    private static Retrofit retrofit;
    public static Retrofit getRetrofit() {
        if(retrofit == null) {
            Retrofit.Builder builder = new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl("xxxx://XXX.com/XX/api/index.php/");
            retrofit = builder.build();
        }
        return retrofit;
    }
}

//Interface

public interface ApiService {
    @Headers("Content-type:application/json")
    @POST("Course/addCourse")
    @FormUrlEncoded
    Call<AddResponse> AddCourse(
            @Field("course_title") String course_title,
            @Field("course_desc") String course_desc,
            @Field("course_fees") String course_fees
    );
}
public class AddResponse {
    @SerializedName("status")
   public Integer status;
    @SerializedName("message")
   public String message;
}

//ViewModel

public class AddCourseViewModel extends ViewModel {
    ActivityMainBinding binding;
    Retrofit retrofit;
    ApiService addService;
    public AddCourseViewModel(ApiService apiService) {
        this.addService = apiService;
    }
    public void AddingCourse() {
        String Course_desc = binding.etCourseDescription.getText().toString();
        String  Course_Title = binding.etCourseTitle.getText().toString();
        String Course_Fees = binding.etCourseFee.getText().toString();
        MutableLiveData<String> msg = new MutableLiveData<>("");
        Call<AddResponse> call = addService.AddCourse(Course_Title,Course_desc,Course_Fees);
        call.enqueue(new Callback<AddResponse>() {
         @Override
         public void onResponse(Call<AddResponse> call, Response<AddResponse> response) {
             if (response.isSuccessful()) {
                 AddResponse addRes = response.body();
                 if (addRes.status == 0) {
                     msg.postValue("Successfully added");
                     return;
                 } else {
                     msg.postValue("Failure 1");
                 }
             }
             else {
                 msg.postValue("Failure 2"+response.code() + "Message " +response.message());
             }
             }

         @Override
         public void onFailure(Call<AddResponse> call, Throwable t) {
              msg.postValue(t.getMessage() +"Failure 3");
         }
     });
 }
}
public class AddCourseVMFactory extends ViewModelProvider.NewInstanceFactory {
    private ApiService apiService;
    public AddCourseVMFactory(ApiService apiService) {
        this.apiService = apiService;
    }
    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        AddCourseViewModel viewModel = new AddCourseViewModel(apiService);
        return ((T)viewModel);
    }
}
public class MainActivity extends AppCompatActivity {
     ActivityMainBinding binding;
     Retrofit retrofit;
     ApiService addService;
    AddCourseVMFactory factory;
    AddCourseViewModel viewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        retrofit = ApiClient.getRetrofit();
        addService = retrofit.create(ApiService.class);
        factory = new AddCourseVMFactory(addService);
        viewModel = new ViewModelProvider(this, factory).get(AddCourseViewModel.class);
        binding.btnAddCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.AddingCourse();
            }
        });
}
Rent Charter Buses Company
READ ALSO
How do I get the value of useState from component A to component B

How do I get the value of useState from component A to component B

I'm relatively new to JS and React as a whole, I delved into React and started making some projects with Django backendI have a list of items in the ProductList component in react,

85
Node application crashes due to mysql error &#39;PROTOCOL_PACKETS_OUT_OF_ORDER&#39;

Node application crashes due to mysql error 'PROTOCOL_PACKETS_OUT_OF_ORDER'

I am running a nodejs application (web service using express) on RHEL v8, using node v16

148
Javascript and jquery I can&#39;t read value from array index

Javascript and jquery I can't read value from array index

I have a list of numbers (ex"20,145") that returns from an ajax call and I am pushing them to a js array

97
I want to filter the clients of every agent individually. Django

I want to filter the clients of every agent individually. Django

I'm creating a view where I can show all the clients of specific agents via there PK

187