在运行时期间出现IllegalStateException预期为Begin Array,但为String

时间:2018-08-20 05:18:55

标签: android illegalstateexception

我向您展示我的代码和JSON模型,请检查并告诉我哪里错了,我在自己的级别上完成了所有操作,但系统向我显示了IllegalstateException。像:-java.lang.IllegalStateException期望BEGIN_ARRAY但在第1行第27列路径处为STRING

class LoginTask extends AsyncTask<Boolean, Void, String> {
    @Override
    protected String doInBackground(Boolean... booleen) {
        UserApi userApi = new UserApi();
        LoginUser result;
        try {
            result = userApi.loginUser(email, pass, fcm_token);
            Log.e("result", String.valueOf(result));
            Gson gson = new Gson();
            String json = gson.toJson(result);
            JSONObject jObj = new JSONObject(json);
            Log.e("result", String.valueOf(jObj));
            if (jObj.getString("error").equalsIgnoreCase("false")) {
                JSONArray array=jObj.getJSONArray("response");
                for(int i = 0; i < array.length(); i++) {
                    JSONObject object = array.getJSONObject(i);                                           
                }return "true";

            } else {
                String errormsg = jObj.getString("response");
                return errormsg;
            }
        } catch (ApiException e) {
            e.printStackTrace();
            Log.e("error", e.toString());
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("error", e.toString());

        } catch (RuntimeException e) {
            e.printStackTrace();
            Log.e("error", e.toString());
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Your email and password not matching to our database.", Toast.LENGTH_LONG).show();
                }
            });
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if (result != null) {
            if (result.equalsIgnoreCase("true")) {
                Log.e("success1", "logged in");
                startActivity(new Intent(MainActivity.this, For_Child_Attendance.class));
                finish();
            } else {
                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
            }
        }
    }
}

这是我的API(LoginUser)的模型:-

{ "error": "string", "response": [ { "id": "string", "email": "string", "fname": "string", "mname": "string", "cname": "string", "dob": "string", "cclass": "string", "fnumber": "string", "mnumber": "string", "address": "string", "cimage": "string", "fcm_token": "string", "forgotten_password_code": "string" } ] }

1 个答案:

答案 0 :(得分:0)

通过这种方式制作pojo类。

public class ResponseItem{

@SerializedName("cclass")
private String cclass;

@SerializedName("fname")
private String fname;

@SerializedName("address")
private String address;

@SerializedName("cname")
private String cname;

@SerializedName("mname")
private String mname;

@SerializedName("cimage")
private String cimage;

@SerializedName("dob")
private String dob;

@SerializedName("mnumber")
private String mnumber;

@SerializedName("fcm_token")
private String fcmToken;

@SerializedName("id")
private String id;

@SerializedName("fnumber")
private String fnumber;

@SerializedName("email")
private String email;

@SerializedName("forgotten_password_code")
private String forgottenPasswordCode;

public void setCclass(String cclass){
    this.cclass = cclass;
}

public String getCclass(){
    return cclass;
}

public void setFname(String fname){
    this.fname = fname;
}

public String getFname(){
    return fname;
}

public void setAddress(String address){
    this.address = address;
}

public String getAddress(){
    return address;
}

public void setCname(String cname){
    this.cname = cname;
}

public String getCname(){
    return cname;
}

public void setMname(String mname){
    this.mname = mname;
}

public String getMname(){
    return mname;
}

public void setCimage(String cimage){
    this.cimage = cimage;
}

public String getCimage(){
    return cimage;
}

public void setDob(String dob){
    this.dob = dob;
}

public String getDob(){
    return dob;
}

public void setMnumber(String mnumber){
    this.mnumber = mnumber;
}

public String getMnumber(){
    return mnumber;
}

public void setFcmToken(String fcmToken){
    this.fcmToken = fcmToken;
}

public String getFcmToken(){
    return fcmToken;
}

public void setId(String id){
    this.id = id;
}

public String getId(){
    return id;
}

public void setFnumber(String fnumber){
    this.fnumber = fnumber;
}

public String getFnumber(){
    return fnumber;
}

public void setEmail(String email){
    this.email = email;
}

public String getEmail(){
    return email;
}

public void setForgottenPasswordCode(String forgottenPasswordCode){
    this.forgottenPasswordCode = forgottenPasswordCode;
}

public String getForgottenPasswordCode(){
    return forgottenPasswordCode;
}
 }

public class ResponseData{

@SerializedName("response")
private List<ResponseItem> response;

@SerializedName("error")
private String error;

public void setResponse(List<ResponseItem> response){
    this.response = response;
}

public List<ResponseItem> getResponse(){
    return response;
}

public void setError(String error){
    this.error = error;
}

public String getError(){
    return error;
}

}

然后尝试这种方式。

        Gson gson = new Gson();
    String json = gson.toJson(result);
    ResponseData result=gson.fromJson(json,ResponseData.class);
    if (result!=null){
        Log.d("Error",result.getError());
    }

否则,您不将json转换为字符串。

仅执行json解析。 请参阅此链接。 https://www.tutorialspoint.com/android/android_json_parser.htm