简体   繁体   English

无法更新成员变量

[英]Can't update member variable

Somehow line mResponseText = response.body().string(); mResponseText = response.body().string(); isn't writing member variable. 没有写成员变量。 Instead it appears to be creating and logging it locally. 相反,它似乎是在本地创建和记录它。

Any ideas why? 有什么想法吗? The more I look at it the more clueless I'm getting :( 我越看越不懂:(

public class Gateway {
    private static final String TAG = Gateway.class.getSimpleName();
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");
    private String mResponseText = "[{'comment' : 'fake' , 'psn_nickname' : 'fake', 'created':'now', 'parent_class':''}]";



    public Gateway (String url, String json,  final Context context) {

        if(isNetworkAvailable(context)) {
            //if network is available build request

            OkHttpClient client = new OkHttpClient();
           // RequestBody body = RequestBody.create(JSON, json);
            Request request = new Request.Builder()
                    .url(url)
                    //.post(body)
                    .build();

            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                //execute call
                @Override
                public void onFailure(Request request, IOException e) {
                    // if request failed
                    Toast.makeText(context, "request failed", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onResponse(Response response) throws IOException {
                    // if succeeded
                    if(response.isSuccessful()){
                        mResponseText = response.body().string();
                        Log.v(TAG, "SETTING RESPONSE");
                        // THIS LOGS PROPER JSON LOADED FROM NETWORK
                        Log.v(TAG, mResponseText);

                    } else {

                        //alertUserAboutError(context);
                        Toast.makeText(context, "Something wrong with response", Toast.LENGTH_LONG).show();
                    }
                }
            });
        } else {
            Toast.makeText(context, "Network is not available", Toast.LENGTH_LONG).show();
        }

    }

    public String getResponse () {
        Log.v(TAG, "GETTING RESPONSE");
        // THIS LOGS FAKE SAMPLE JSON --- WTF???
        Log.v(TAG, mResponseText);
        return mResponseText;
    }


    // check if network is available
    private boolean isNetworkAvailable(Context c) {
        ConnectivityManager manager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;
        }

        return isAvailable;
    }
    /*
    private void alertUserAboutError(Context c) {
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(c.getFragmentManager(), "error_dialog");
    }
    */
}

Here's the code that's using this class 这是使用此类的代码

Gateway gateway = new Gateway(mCommentURL, "", this);
String mJsonData = gateway.getResponse();

EDIT Code update - removed extends Activity 编辑代码更新-删除extends Activity

You're calling getResponse() too early. 您太早调用getResponse()了。 The async operation has not completed yet and the value returned is the one you initialize there in the first place, not the one written in the Callback . 异步操作尚未完成,返回的值是您首先在此处初始化的值,而不是在Callback编写的值。

Put the code that uses the response in the Callback , or call that code from the callback. 将使用响应的代码放在Callback ,或从回调中调用该代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CountDownTimer onTick()无法更新类的成员 - CountDownTimer onTick() can't update a member of class Android Studio 无法更新公共变量 - Android Studio can't update public variable 如何更新托管bean的属性成员变量? - How to update the property member variable of a managed bean? 合并失败,无法使用byte []成员变量更新Entity - Merge failing to update Entity with byte[] member variable 类无法使用来自不同包的反射来访问其自己的受保护的成员变量 - Class can't access its own protected member variable using reflection from different package 为什么我的Custom对象中不能有几个哈希图作为成员变量 - How come I can't have several hashmaps as member variable in my Custom object 如何创建一个 ImmutableList 作为成员变量? - How can I create an ImmutableList as a member variable? 为什么我的类的自定义成员变量在其他类中不更新? - Why does my custom member variable of a class not update in other classes? LinkedList的addAfter方法不会更新下一个节点的上一个成员变量 - LinkedList addAfter method does not update the next nodes previous member variable Immutable Object with ArrayList 成员变量 - 为什么这个变量可以改变? - Immutable Object with ArrayList member variable - why can this variable be changed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM