java.lang.illegalstateexception预期为begin_object,但进行了字符串改装

时间:2019-05-25 12:07:55

标签: android mysql retrofit2

我正在设置新的用户注册,并希望使用翻新2库将数据保存在Mysql数据库中

MainActivity

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_reg, container, false);
    Username = view.findViewById(R.id.username);
    Email = view.findViewById(R.id.mail);
    Pass = view.findViewById(R.id.pswrdd);
    Fname = view.findViewById(R.id.firstname);
    Lname = view.findViewById(R.id.lastname);
    Mobile = view.findViewById(R.id.mobphone);
    Address = view.findViewById(R.id.mobaddress);
    btn = view.findViewById(R.id.sup);
    textView = view.findViewById(R.id.lin);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_id, new LoginFragment()).addToBackStack(null).commit();
        }
    });
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Reg();
        }
    });
    return view;
}
    public void Reg(){
     String username = Username.getText().toString();
     String email = Email.getText().toString();
     String pass = Pass.getText().toString();
     String fname = Fname.getText().toString();
     String lname = Lname.getText().toString();
     String mobile = Mobile.getText().toString();
     String address = Address.getText().toString();
     Call<User> call = MainActivity.apiInterface.performUserRegistration(email, username, pass, fname, lname, mobile, address);
call.enqueue(new Callback<User>() {
    @Override
    public void onResponse(Call<User> call, Response<User> response) {
        if(response.body().getResponse().equals("ok")){
            MainActivity.prefConfig.DisplayToast("Success");
        }

    }

    @Override
    public void onFailure(Call<User> call, Throwable t) {
        Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
    }
});
}
}

ApiClient.java

public static Retrofit getRetrofit(){
    if(retrofit == null){
       Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        retrofit = new Retrofit.Builder().
                baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }
    return retrofit;
  }
}

ApiInterface

Call<User> performUserRegistration(@Query("email") String Email, @Query("username") String UserName, @Query("pass") String UserPass, @Query("firstname") String FirstName, @Query("lastname") String LastName, @Query("mobile") String Mobile, @Query("address") String Address);


  public class User {
@SerializedName("response")
private String Response;
@SerializedName("name")
private String Name;
public String getName()
{
    return Name;
}
public  String getResponse(){
    return Response;
}

}

我得到的错误是这个

  

“ java.lang.IllegalSatateException:预期为BEGIN_OBJECT,但为字符串   在第1行第1列的路径$“

但是数据已成功存储在数据库中。请任何人可以帮助我确定错误背后的原因是什么。

0 个答案:

没有答案