com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在Android中为BEGIN_OBJECT

维维克·沙(Vivek Shah)

我正在尝试使用如下所示的bean类来解析Json数据,但出现错误“ com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但为BEGIN_OBJECT”

JSON数据

{
"response": true,
"message": "Success",
"Data": [
    {
        "laboratory_id": 1000,
        "laboratory_image": "http://abcd.com/images/laboratory.jpg",
        "laboratory_name": "Laboratory & Research Institute",
        "laboratory_address": "Cross Road, ",
        "laboratory_rating": 2.5,
        "laboratory_profile": {
            "head_name": "Ramesh Jain",
            "degree_name": "M.D",
            "speciality": "Lab Technology",
            "license_number": "34343434",
            "laboratory": "Laboratory"
        }
    },
    {
        "laboratory_id": 1001,
        "laboratory_image": "http://abcd.com/images/laboratory.jpg",
        "laboratory_name": "XYZ LAB",
        "laboratory_address": "ASHKA ROAD",
        "laboratory_rating": 0,
        "laboratory_profile": {
            "head_name": "MR X",
            "degree_name": "MBBS",
            "speciality": null,
            "license_number": "123456",
            "laboratory": "Diagnostic"
        }
    }

]

}

实验室列表模型

公共类LaboratoryListModel {

private String response;
private String message;
private ArrayList<LaboratoryList> Data = new ArrayList<LaboratoryList>();


public String getResponse() {
    return response;
}

public void setResponse(String response) {
    this.response = response;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public ArrayList<LaboratoryList> getData() {
    return Data;
}

public void setData(ArrayList<LaboratoryList> data) {
    Data = data;
}

}

LaboratoryList
公共类LaboratoryList {

private String laboratory_id;
private String laboratory_image;
private String laboratory_name;
private String laboratory_address;
private String laboratory_rating;

private ArrayList<LaboratoryProfile> laboratory_profile = new ArrayList<LaboratoryProfile>();

public ArrayList<LaboratoryProfile> getLaboratory_profile() {
    return laboratory_profile;
}

public void setLaboratory_profile(
        ArrayList<LaboratoryProfile> laboratory_profile) {
    this.laboratory_profile = laboratory_profile;
}

public String getLaboratory_id() {
    return laboratory_id;
}

public void setLaboratory_id(String laboratory_id) {
    this.laboratory_id = laboratory_id;
}

public String getLaboratory_image() {
    return laboratory_image;
}

public void setLaboratory_image(String laboratory_image) {
    this.laboratory_image = laboratory_image;
}

public String getLaboratory_name() {
    return laboratory_name;
}

public void setLaboratory_name(String laboratory_name) {
    this.laboratory_name = laboratory_name;
}

public String getLaboratory_address() {
    return laboratory_address;
}

public void setLaboratory_address(String laboratory_address) {
    this.laboratory_address = laboratory_address;
}

public String getLaboratory_rating() {
    return laboratory_rating;
}

public void setLaboratory_rating(String laboratory_rating) {
    this.laboratory_rating = laboratory_rating;
}

}

实验室简介

public class LaboratoryProfile {

private String head_name;
private String degree_name;
private String speciality;
private String license_number;
private String laboratory;

public String getHead_name() {
    return head_name;
}

public void setHead_name(String head_name) {
    this.head_name = head_name;
}

public String getDegree_name() {
    return degree_name;
}

public void setDegree_name(String degree_name) {
    this.degree_name = degree_name;
}

public String getSpeciality() {
    return speciality;
}

public void setSpeciality(String speciality) {
    this.speciality = speciality;
}

public String getLicense_number() {
    return license_number;
}

public void setLicense_number(String license_number) {
    this.license_number = license_number;
}

public String getLaboratory() {
    return laboratory;
}

public void setLaboratory(String laboratory) {
    this.laboratory = laboratory;
}

}

网络服务

public class GetLaboratoryListTask extends
        AsyncTask<String, Integer, Object> {

    private final Context mContext;
    private final ProgressDialog mProgressDialog;
    private ListView mlistView;

    LaboratoryListModel labListModel;

    public GetLaboratoryListTask(final Context mContext, ListView listView) {
        this.mContext = mContext;
        mlistView = listView;
        mProgressDialog = new ProgressDialog(mContext);
        mProgressDialog.setMessage("Please wait..");
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setCancelable(false);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }

    @Override
    protected Object doInBackground(String... params) {

        LaboratoryListModel laboratoryListModel = (LaboratoryListModel) getLaboratoryList(params[0]);
        if (laboratoryListModel != null)
            return laboratoryListModel.getData();
        else
            return null;

    }

    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }

        if (result != null) {
            if (labListModel.getResponse().equalsIgnoreCase("true")) {
                if (result != null && result instanceof List) {
                    mlistView.setAdapter(new SearchLabAdapter(
                            LabListActivity.this,
                            (List<LaboratoryList>) result));
                }
            } else {

            }
        }

    }

    private Object getLaboratoryList(String category_id) {
        String webUrl = Constant.URL_SEARCH_LABORATORY;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(),
                    10000);
            HttpResponse response;
            JSONObject object = new JSONObject();
            JSONObject objectContacts = new JSONObject();
            try {
                HttpPost post = new HttpPost(Constant.URL_SEARCH_LABORATORY);

                objectContacts.put(Constant.CITY, "");
                objectContacts.put(Constant.LOCATION_ZIPCODE, "");
                objectContacts.put(Constant.LABORATORY_NAME, "");

                StringEntity se = new StringEntity(
                        objectContacts.toString());

                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                post.setEntity(se);
                response = client.execute(post);

                if (response != null) {
                    //
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(response.getEntity()
                                    .getContent(), "UTF-8"));
                    String json1 = reader.readLine();
                    JSONTokener tokener = new JSONTokener(json1);
                    jObject = new JSONObject(tokener);

                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            labListModel = (LaboratoryListModel) new Gson().fromJson(
                    jObject.toString(), LaboratoryListModel.class);

            return labListModel;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

由于我是新手,所以我不知道这是什么问题,我使用过Google,但没有解决方案,有人可以帮我吗?

托马斯·R。

关于您的评论。您需要更改模型。您的LaboratoryList应该只包含一个配置文件。

更改为:

private LaboratoryProfile laboratory_profile;

public LaboratoryProfile getLaboratory_profile() {
    return laboratory_profile;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档