Reputation: 392
I have read previous solutions mostly this one for this problem but none of them worked for me. :(. While debugging i found raw request data from retrofit something like below picture and I've highlighted 2 portions:
here the 1st highlighted portion showing my retrofit request in raw json format
{
"operation": "register",
"studentModel": {
"batch_id": "cseuui",
"dept_code": "CSE",
"password": "p",
"student_address": "tpc",
"student_email": "[email protected]",
"student_id": "tyjvc",
"student_name": "jak",
"student_phone": "87532"
}
}
but which giving me the 2nd highlighted portion of error:
Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
But using postman i was able to successfully insert this raw json in the database.
{
"operation": "register",
"studentModel": {
"batch_id": "cseuui",
"dept_code": "CSE",
"password": "p",
"student_address": "tpc",
"student_email": "[email protected]",
"student_id": "tyjvc",
"student_name": "jak",
"student_phone": "87532"
}
}
That means my server side is ok.
Is there any thing else i am missing?
I am giving some of my codes below,
Retrofit2 and Gson Dependency:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Retrofit Client and ApiInterface
and finally
as my server side is working perfectly with postman, what is the thing i am missing here. please let me know.
Upvotes: 1
Views: 594
Reputation: 936
Create Model class Simple not list type.
after that create a String variable operation and Create Inner class of studentModel .
public class Main{
@SerializeName("operation")
private String operation;
@SerializeName("StudentModel")
private StudentModel studentModel;
}
and then generate getter setter
Upvotes: 0
Reputation: 392
Finally I solved this problem, just by changing student model class variable names as my database column names, and i also removed @SerializedName("result")
@Expose
mapping in the class definition.
Actually Database column names were not matching with my class variables.
Thank you all, for your effort.
Upvotes: 1
Reputation: 3539
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'
Upvotes: 0
Reputation: 2082
you are trying to access array but Gson finds a string in the first line.
so you need to change the model class.
StudentModel.java
` public class StudentModel {
private String operation;
private Student student;
public Rootjava(String operation, Student student) {
this.operation = operation;
this.student = student;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
@Override
public String toString() {
return "studentModel{" +
"operation='" + operation + '\'' +
", student=" + student +
'}';
}
private class Student{
@SerializedName("dept_code")
private String deptCode;
@SerializedName("student_name")
private String studentName;
private String password;
@SerializedName("batch_id")
private String batchId;
@SerializedName("student_phone")
private String studentPhone;
@SerializedName("student_address")
private String studentAddress;
@SerializedName("student_id")
private String studentId;
@SerializedName("student_email")
private String studentEmail;
public Student(String deptCode, String studentName,
String password, String batchId,
String studentPhone, String studentAddress,
String studentId, String studentEmail) {
this.deptCode = deptCode;
this.studentName = studentName;
this.password = password;
this.batchId = batchId;
this.studentPhone = studentPhone;
this.studentAddress = studentAddress;
this.studentId = studentId;
this.studentEmail = studentEmail;
}
public String getDeptCode() {
return deptCode;
}
public void setDeptCode(String deptCode) {
this.deptCode = deptCode;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBatchId() {
return batchId;
}
public void setBatchId(String batchId) {
this.batchId = batchId;
}
public String getStudentPhone() {
return studentPhone;
}
public void setStudentPhone(String studentPhone) {
this.studentPhone = studentPhone;
}
public String getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(String studentAddress) {
this.studentAddress = studentAddress;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentEmail() {
return studentEmail;
}
public void setStudentEmail(String studentEmail) {
this.studentEmail = studentEmail;
}
@Override
public String toString() {
return "Student{" +
"deptCode='" + deptCode + '\'' +
", studentName='" + studentName + '\'' +
", password='" + password + '\'' +
", batchId='" + batchId + '\'' +
", studentPhone='" + studentPhone + '\'' +
", studentAddress='" + studentAddress + '\'' +
", studentId='" + studentId + '\'' +
", studentEmail='" + studentEmail + '\'' +
'}';
}
}
}`
Use this model class.
you can use the plugin to generate this POJO class.
Upvotes: 0
Reputation: 884
Retrofit Getting InValid JSON Responce
try to Send Responce In this String Fromat from Server
Gson gson = new Gson();
response=gson.toJson(courseList);
JsonObject myObj = new JsonObject();
myObj.addProperty("operation", "register");
myObj.add("studentModel",data);
response = myObj.toString();
return responce;
Retrofit Builder
retrofit =new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Upvotes: 0