Try this:
Your reciving string instead of JsonObject
.Change your ServerResponse
like this mapping and add header as 'application/json'
1.Add headder header.put("Content-Type", "application/json");
private void registerProcess(String studentName, String studentAddress, String studentEmail, String studentPhone, String department, String batch_id, String student_id, String rePassword) {
APIInterface appInterface = ApiClient.getClient().create(APIInterface.class);
StudentModel student = new StudentModel();
student.setStudent_name(studentName);
student.setStudent_address(studentAddress);
student.setStudent_email(studentEmail);
student.setStudent_phone(studentPhone);
student.setDept_code(department);
student.setBatch_id(batch_id);
student.setStudent_id(student_id);
student.setPassword(rePassword);
Log.e(TAG,student.toString());
ServerRequest request = new ServerRequest();
Map<String, String> header = new HashMap<>();
header.put("Content-Type", "application/json");
request.setOperation(Config_Ref.REGISTER_OPERATION);
request.setStudentModel(student);
Call<ServerResponse> response = appInterface.operation(request,header);
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
if(response.isSuccessful()){
ServerResponse resp = response.body();
Toast.makeText(RegisterActivity.this, resp.getMessage()+" success", Toast.LENGTH_SHORT).show();
Log.e(TAG,resp.getMessage()+" success");
}else{
Toast.makeText(RegisterActivity.this, response.message()+" not success", Toast.LENGTH_SHORT).show();
Log.e(TAG,response.message()+" not success");
}
/* Snackbar.make(fview, resp.getMessage(), Snackbar.LENGTH_LONG).show();*/
//pDialog.dismiss();
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
/*Snackbar.make(fview, t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();*/
Toast.makeText(RegisterActivity.this, t.getLocalizedMessage()+" failed"+call.toString(), Toast.LENGTH_SHORT).show();
Log.e(TAG,t.getLocalizedMessage()+" failed");
}
});
}
2.ApiInterface.java
Add header as json format
public interface ApiInterface {
@POST("/iuk/api/credentials")
Call<ServerResponse> operation(@Body ServerRequest request,@HeaderMap Map<String, String> header);
}
3.ServerResponse.java
public class ServerResponse {
@SerializedName("result")
@Expose
private String result;
@SerializedName("message")
@Expose
private String message;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
ApiClient.Java
public class ApiClient {
private static final String BASE_URL = "https://xyz.000webhostapp.com";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder requestBuilder = chain.request().newBuilder();
requestBuilder.header("Content-Type", "application/json");
return chain.proceed(requestBuilder.build());
}
})
.connectTimeout(30, TimeUnit.MINUTES)
.readTimeout(30, TimeUnit.MINUTES)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Depedencies
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'