first of all I know that this question has already been asked many times. But I cannot find a solution to the problem. From the log I can see that the web service correctly returns the JSON to me. But for some reason it never enters the onResponse method. I would be very grateful if someone could give me a hint on this matter.
package com.example.hms_android_application;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.hms_android_application.Retrofit.Account;
import com.example.hms_android_application.Retrofit.INodeJS;
import com.example.hms_android_application.Retrofit.RetrofitClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class ProfileActivity extends AppCompatActivity {
TextView edt_firstq,
edt_midq,
edt_famq,
edt_phoneq,
edt_adressq,
edt_usernameq,
edt_passwordq, edt_egnq,edt_bloodq,edt_genderq;
Button btn_regs;
private String u_username;
private Account account;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
edt_usernameq = findViewById(R.id.textView13);
edt_passwordq = findViewById(R.id.textView14);
edt_adressq = findViewById(R.id.textView15);
edt_egnq = findViewById(R.id.textView16);
edt_famq = findViewById(R.id.textView17);
edt_firstq = findViewById(R.id.textView18);
edt_phoneq = findViewById(R.id.textView19);
edt_midq = findViewById(R.id.textView20);
edt_bloodq = findViewById(R.id.textView21);
edt_genderq = findViewById(R.id.textView22);
btn_regs = (Button)findViewById(R.id.btn_save);
loadData();
}
private void loadData(){
Intent intent = getIntent();
u_username = intent.getStringExtra("u_username");
INodeJS accountservice = RetrofitClient.getInstance().create(INodeJS.class);
accountservice.find(u_username).enqueue(new Callback<Account>() {
@Override
public void onFailure(Call<Account> call, Throwable t) {
Toast.makeText(getApplicationContext(), getString(R.string.createfield), Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Call<Account> call, Response<Account> response) {
try {
if(response.isSuccessful()){
account = response.body();
edt_firstq.setText(account.getpFirstName());
edt_usernameq.setText(account.getuUsername());
edt_adressq.setText(account.getpAddress());
edt_egnq.setText(account.getpEgn());
edt_famq.setText(account.getpFamilyName());
edt_phoneq.setText(account.getpPhone());
edt_midq.setText(account.getpMidName());
edt_bloodq.setText(account.getpBloodGroup());
edt_genderq.setText(account.getpGender());
edt_passwordq.setText(account.getuUsername());
}
else {
Toast.makeText(getApplicationContext(), getString(R.string.app_name), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
RetrofitClient
public class RetrofitClient {
private static Retrofit instance = null;
public static Retrofit getInstance(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
instance = new Retrofit.Builder()
.baseUrl("http://192.168.100.103:3000/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return instance;
}
}
Interface
public interface INodeJS {
@POST("login/")
Call<Boolean> login(@Body Account account);
@POST("register/")
Call<Boolean> create(@Body Account account);
@GET("user/{u_username}")
Call<Account> find(@Path("u_username") String u_username);
}
Account
public class Account implements Serializable {
@SerializedName("p_first_name")
@Expose
private String pFirstName;
@SerializedName("p_mid_name")
@Expose
private String pMidName;
@SerializedName("p_family_name")
@Expose
private String pFamilyName;
@SerializedName("p_egn")
@Expose
private String pEgn;
@SerializedName("p_gender")
@Expose
private String pGender;
@SerializedName("p_phone")
@Expose
private String pPhone;
@SerializedName("p_address")
@Expose
private String pAddress;
@SerializedName("p_blood_group")
@Expose
private String pBloodGroup;
@SerializedName("u_password")
@Expose
private String uPassword;
@SerializedName("u_username")
@Expose
private String uUsername;
public String getpFirstName() {
return pFirstName;
}
public void setpFirstName(String pFirstName) {
this.pFirstName = pFirstName;
}
public String getpMidName() {
return pMidName;
}
public void setpMidName(String pMidName) {
this.pMidName = pMidName;
}
public String getpFamilyName() {
return pFamilyName;
}
public void setpFamilyName(String pFamilyName) {
this.pFamilyName = pFamilyName;
}
public String getpEgn() {
return pEgn;
}
public void setpEgn(String pEgn) {
this.pEgn = pEgn;
}
public String getpGender() {
return pGender;
}
public void setpGender(String pGender) {
this.pGender = pGender;
}
public String getpPhone() {
return pPhone;
}
public void setpPhone(String pPhone) {
this.pPhone = pPhone;
}
public String getpAddress() {
return pAddress;
}
public void setpAddress(String pAddress) {
this.pAddress = pAddress;
}
public String getpBloodGroup() {
return pBloodGroup;
}
public void setpBloodGroup(String pBloodGroup) {
this.pBloodGroup = pBloodGroup;
}
public String getuPassword() {
return uPassword;
}
public void setuPassword(String uPassword) {
this.uPassword = uPassword;
}
public String getuUsername() {
return uUsername;
}
public void setuUsername(String uUsername) {
this.uUsername = uUsername;
}
}
Log out
I/okhttp.OkHttpClient: --> GET http://192.168.100.103:3000/user/mariq12
I/okhttp.OkHttpClient: --> END GET
I/zygote: Do full code cache collection, code=78KB, data=93KB
I/zygote: After code cache collection, code=76KB, data=69KB
I/okhttp.OkHttpClient: <-- 200 OK http://192.168.100.103:3000/user/mariq12 (162ms)
X-Powered-By: Express
I/okhttp.OkHttpClient: Content-Type: application/json; charset=utf-8
Content-Length: 290
I/okhttp.OkHttpClient: ETag: W/"122-zx+JBLEne+i9Ys0+0Y2w7hRX3RM"
Date: Thu, 17 Dec 2020 17:04:52 GMT
I/okhttp.OkHttpClient: Connection: keep-alive
Keep-Alive: timeout=5
I/okhttp.OkHttpClient: [{"p_first_name":"Мария","p_mid_name":"Стоянова","p_family_name":"Петрова","p_egn":"8502018520","p_gender":"Жена","p_phone":"0873025145","p_address":"\"ул.Тодор Петров 22\" Варна","p_blood_group":"A+","u_password":"mariq12","u_username":"mariq12"}]
I/okhttp.OkHttpClient: <-- END HTTP (290-byte body)
D/EGL_emulation: eglMakeCurrent: 0xd6f2aba0: ver 2 0 (tinfo 0xe587bd30)
I/chatty: uid=10079(com.example.hms_android_application) RenderThread identical 1 line
D/EGL_emulation: eglMakeCurrent: 0xd6f2aba0: ver 2 0 (tinfo 0xe587bd30)
D/EGL_emulation: eglMakeCurrent: 0xd6f2aba0: ver 2 0 (tinfo 0xe587bd30)
D/EGL_emulation: eglMakeCurrent: 0xd6f2aba0: ver 2 0 (tinfo 0xe587bd30)
I tried to see what a mistake it was.
public void onFailure(Call<Account> call, Throwable t) {
t.printStackTrace();
}
The mistake he makes is
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $