如何获取值data.length()到Ion(HTTP LIB:Library)?

时间:2017-01-11 04:43:10

标签: android

这是我的代码:

    String url = "http://x.x.x.x/docid.php" ;
    try {
        JSONArray data = new JSONArray(getJSONUrl(url));
        final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        map = new HashMap<String, String>();
        Ion.with(this)
                .load("http://domain.com/url.php")
                .asString()
                .withResponse()
                .setCallback(new FutureCallback<Response<String>>() {
                    @Override
                    public void onCompleted(Exception e, Response<String> result) {
                        result.getResult();
                        if(data.length()==0){
                            (why show error for data.length()?)

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

在Ion Library我想检查“url =”http://x.x.x.x/docid.php“;”中的data.length()在离子库中,但错误警告“从内部类中访问本地变量数据”。如何修复它?谢谢。

2 个答案:

答案 0 :(得分:1)

尝试将数据设置为final

例如

    String url = "http://x.x.x.x/docid.php" ;
    try {
        final JSONArray data = new JSONArray(getJSONUrl(url));
        final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        map = new HashMap<String, String>();
        Ion.with(this)
                .load("http://domain.com/url.php")
                .asString()
                .withResponse()
                .setCallback(new FutureCallback<Response<String>>() {
                    @Override
                    public void onCompleted(Exception e, Response<String> result) {
                        result.getResult();
                        if(data.length()==0){
                            (why show error for data.length()?)

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

答案 1 :(得分:1)

如果要从内部类访问它,则需要将variable转换为Final。将您的data更改为Final

 final JSONArray data = new JSONArray(getJSONUrl(url));