安卓onResponse方法从未被称为Reformation2



首先我知道这个问题已经被问了很多次了。但我找不到解决问题的办法。从日志中,我可以看到web服务正确地向我返回了JSON。但由于某种原因,它从未进入onResponse方法。如果有人能在这件事上给我一个提示,我将不胜感激。

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();
}
}
});
}
}

改装客户

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;
}
}

接口

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);
}

账户

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;
}
}

注销

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)

我试着看看这是个什么错误。

public void onFailure(Call<Account> call, Throwable t) {
t.printStackTrace();
}

他犯的错误是

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ 

首先,尝试记录onResponse((方法。所以在你的onResponse方法中,你应该添加这行

Log.d("ResponseTAG",  "onResponse() called: " + response.code());

然后试着在你的日志中找到这个日志。运行您的代码,转到logcat,在那里过滤您的TAG。

您的logcat应该包含以下筛选器:1

如果您在日志文件中找到以下文本:onResponse((已调用:RESPONSE_CODE这意味着您的onResponce((已被调用,但出现了一些异常。因为你捕捉到了所有的异常

try {

catch (Exception e){
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}

try块中的代码不起作用。所以尝试捕获其他Exception类。

您得到JsonSyntaxException:应为BEGIN_OBJECT,但却是BEGIN_ARRAY

这意味着:您的JSON响应包含JSONArray,而不是JSONObject。在您的案例中,您有一系列帐户模型。不是特定用户,而是用户列表。所以你可以创建ArrayList f.e.

private List<Account> photoArrayList = new ArrayList<>();

并在那里加载您的Account类模型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<List<Account>>() {
@Override
public void onFailure(Call<List<Account>> call, Throwable t) {
Toast.makeText(getApplicationContext(), getString(R.string.createfield), Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Call<List<Account>> call, Response<Account> response) {
if (response.isSuccessful()) {
yourArrayList.addAll(response.body);
}

}
});
}

如果它再次不起作用,请尝试更改接口方法

@GET("user/{u_username}")
Call<List<Account>> find(@Path("u_username") String u_username);

最后你可以阅读这个答案

最新更新