在异步调用中更改变量

时间:2018-08-16 17:26:12

标签: java android boolean okhttp3

我正在尝试解析从response中获得的OkHttp3Connection,它几​​乎总是返回String的值"true",它的值是names[0]这个例子。

现在,我尝试根据响应将字符串转换为布尔值并返回true/false。现在,我猜想由于OkHttp3Connection需要花费一些时间,因此它不会让查询结束并返回默认值false

我要说的是,无论字符串false是什么,此函数始终返回names[0]

public boolean checkIfSongPurhcased(String songName){

    final boolean[] songCheckResult = new boolean[1];

    OkHttp3Connection.doOkHttp3Connection("", Services_Url_class.GET_SONG_PURCHASED, OkHttp3Connection.Request_type.POST, request_data, new OkHttp3Connection.OkHttp3RequestCallback() {
                @Override
                public void onSuccess(String result, String userTag) throws JSONException {
                    JSONObject jsonResult = new JSONObject(result);
                    JSONArray data = jsonResult.getJSONArray("data");
                    if(data != null) {
                        String[] names = new String[data.length()];
                        for(int i = 0 ; i < data.length() ; i++) {
                            JSONArray arr = data.getJSONArray(i);
                            JSONObject dataObj = arr.getJSONObject(0);
                            Iterator<String> keys = dataObj.keys();
                            names[i] = dataObj.getString(keys.next());
                        }

                        //names[0] is "true"
                        songCheckResult[0] = Boolean.parseBoolean(names[0]);
                    }
                }
                @Override
                public void onError(String error, String userTag) {
                    System.out.println("songy" + error);
                }
            }
    );

    //always returns false
    return songCheckResult[0];

如果names[0] is "true"可以使我成真,我该怎么做。

2 个答案:

答案 0 :(得分:3)

您应该传递侦听器/回调。

首先创建如下所示的回调接口

public interface OnQueryResultCallback{
   void onQueryResult(String songName, boolean status);
}

然后将侦听器/回调传递给您的方法,这是经过修改的方法。

public void checkIfSongPurhcased(final String songName, final OnQueryResultCallback listener){
    OkHttp3Connection.doOkHttp3Connection("", Services_Url_class.GET_SONG_PURCHASED, OkHttp3Connection.Request_type.POST, request_data, new OkHttp3Connection.OkHttp3RequestCallback() {
                @Override
                public void onSuccess(String result, String userTag) throws JSONException {
                    JSONObject jsonResult = new JSONObject(result);
                    JSONArray data = jsonResult.getJSONArray("data");
                    if(data != null) {
                        String[] names = new String[data.length()];
                        for(int i = 0 ; i < data.length() ; i++) {
                            JSONArray arr = data.getJSONArray(i);
                            JSONObject dataObj = arr.getJSONObject(0);
                            Iterator<String> keys = dataObj.keys();
                            names[i] = dataObj.getString(keys.next());
                        }

                        //names[0] is "true"
                        boolean status = Boolean.parseBoolean(names[0]);
                        listener.onQueryResult(songName,status);
                    }
                }
                @Override
                public void onError(String error, String userTag) {
                    System.out.println("songy" + error);
                    listener.onQueryResult(songName,false);
                }
            }
    );
}

然后按如下所示调用checkIfSongPurhcased

checkIfSongPurhcased("songName", new OnQueryResultCallback(){
   public void onQueryResult(String songName, boolean status){
        if(status){
          // do something on success
        }else{
         // do something on failure
        }
   }
});

答案 1 :(得分:1)

尝试使用getBoolean()而不是getString()。

例如:names[i] = dataObj.getBoolean(keys.next());