android - Fastjson 解析同一个字符串第二次报classcast exception
阿神
阿神 2017-04-17 14:00:59
0
0
790

问题是这样的:我利用AsyncHttpClient向服务器请求列表json,利用fastjson解析成一个对象然后展示在listview中,长按listview的item告诉服务器删除这个item 再重新请求列表json,第二次解析的时候就报
java.lang.ClassCastException: com.alibaba.fastjson.JSONArray cannot be cast to java.util.ArrayList
错误了
json格式如下:
"{ \"count\": 3, \"data\":[ { \"host_id\": 4, \"id\": 1, \"name\": \"阳光小区\", \"community_province\": \"浙江省\" ,\"community_city\": \"宁波市\" ,\"community_district\": \"鄞州区\" }, { \"host_id\": 4, \"id\": 2, \"name\": \"梦妃2号公寓\",\"community_province\": \"浙江省\", \"community_city\": \"宁波市\", \"community_district\": \"鄞州区\" }, { \"host_id\": 4, \"id\": 3, \"name\": \"house2小区\", \"community_province\": \"浙江省\", \"community_city\": \"宁波市\", \"community_district\": \"鄞州区\" } ], \"status\": \"ok\" }";


activity代码如下

public class CommunityActivity extends BaseActivity implements AdapterView.OnItemLongClickListener {

    @InjectView(R.id.error_layout)
    EmptyLayout emptyLayout;

    @InjectView(R.id.list)
    ListView listView;

    private ArrayList<Community> communityList;

    private CommunityAdapter communityAdapter;



    private AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {


            emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);

            try {

                String response ="{ \"count\": 3, \"data\":[ { \"host_id\": 4, \"id\": 1, \"name\": \"阳光小区\", \"community_province\": \"浙江省\" ,\"community_city\": \"宁波市\" ,\"community_district\": \"鄞州区\" }, { \"host_id\": 4, \"id\": 2, \"name\": \"梦妃2号公寓\",\"community_province\": \"浙江省\", \"community_city\": \"宁波市\", \"community_district\": \"鄞州区\" }, { \"host_id\": 4, \"id\": 3, \"name\": \"house2小区\", \"community_province\": \"浙江省\", \"community_city\": \"宁波市\", \"community_district\": \"鄞州区\" } ], \"status\": \"ok\" }";
                        //new String(responseBody);



                //转换成对象

                Result<ArrayList<Community>> result = JSON.parseObject(response, new TypeReference<Result<ArrayList<Community>>>() {
                });

                if (result.getStatus().equals("ok")) {

                    communityList = result.getData();
                    System.out.println(JSON.toJSONString(communityList));

                    if (communityAdapter == null) {
                        listView.setAdapter(new CommunityAdapter(communityList));
                    } else {
                        communityAdapter.setCommunityList(communityList);
                        communityAdapter.notifyDataSetChanged();
                    }

                } else {
                    onFailure(statusCode, headers, responseBody, new Throwable());
                }
            } catch (Exception e) {
                e.printStackTrace();
                onFailure(statusCode, headers, responseBody, e);
            }


        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            AppContext.showToastShort("数据获取失败~!");

        }
    };



    private AsyncHttpResponseHandler deleteHandler = new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

            emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);

            try {

                String response = new String(responseBody);

                //转换成对象

                Result result = JSON.parseObject(response, Result.class);

                if (result.getStatus().equals("ok")) {

                    AppContext.showToastShort("删除成功~!");

                } else {
                    AppContext.showToastShort("删除失败~!");
                }
                InyouziApi.getCommunities(handler);

            } catch (Exception e) {
                e.printStackTrace();
                onFailure(statusCode, headers, responseBody, e);
            }


        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            InyouziApi.getCommunities(handler);
            AppContext.showToastShort("删除失败~!");

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setDisplayShowCustomEnabled(true);
        mActionBar.setCustomView(R.layout.actionbar_custom_view);
        View view = mActionBar.getCustomView();
        TextView textView = (TextView) view.findViewById(R.id.actionbar_title);
        textView.setText(R.string.xuanzexiaoqu);


        getMenuInflater().inflate(R.menu.menu_community, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_add) {

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected boolean hasBackButton() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_community;
    }

    @Override
    public void initView() {

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sendRequestData();
            }
        });

        listView.setOnItemLongClickListener(this);

    }

    @Override
    public void initData() {

        sendRequestData();


    }

    @Override
    public void onClick(View v) {

    }

    private void sendRequestData() {

        if (!TDevice.hasInternet()) {
            AppContext.showToastShort(R.string.tip_no_internet);
            emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            return;
        }

        emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);

        InyouziApi.getCommunities(handler);

    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) {

        DialogHelp.getConfirmDialog(this, "确定删除该小区?", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                if (!TDevice.hasInternet()) {
                    AppContext.showToastShort(R.string.tip_no_internet);
                    return;
                }

                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                InyouziApi.deleteCommunity((int) id, deleteHandler);
            }
        }).show();

        return false;
    }
}
阿神
阿神

闭关修行中......

모든 응답(0)
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿