简体   繁体   English

Java Json 请求 Android

[英]Java Json Request Android

public class BtcPaymentQR extends AppCompatActivity {公共类 BtcPaymentQR 扩展 AppCompatActivity {

ImageView QrCode;
RequestQueue mQueue;
public static String btcAddress = "";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_btc_payment_qr);

    QrCode = findViewById(R.id.imageViewQR);
    mQueue = Volley.newRequestQueue(this);
    getAddress();

    Log.i("test", "onCreate: " + btcAddress);

}

public void getAddress(){
    String url = "xxx"

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONObject jsonObject = response.getJSONObject("address");
                        String a = jsonObject.getString("extkey_next_receiving_address");
                        BtcPaymentQR.btcAddress=a;


                        Log.i("test", "onResponse: " + btcAddress);

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

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    mQueue.add(request);
}

Logcat逻辑猫

My Question is, How do I get the value of "a" from the method.我的问题是,如何从方法中获取“a”的值。 I've tried everything to save it in another global variable, with return, with get and set.我已经尝试了一切将它保存在另一个全局变量中,返回,获取和设置。 I need the value to use it in a other Method我需要在其他方法中使用它的价值

Simple way is using static variable.简单的方法是使用静态变量。 define a static variable for class and assign the value of 'a' to this static variable and use it every where.为类定义一个静态变量并将“a”的值分配给这个静态变量并在任何地方使用它。

Your problem is solved by using static variable to save response,and static variables are part of class not object so you can use static variable with class name like MainActivity.myValue in my example.您的问题是通过使用静态变量来保存响应来解决的,并且静态变量是类的一部分而不是对象,因此您可以在我的示例中使用类名称为MainActivity.myValue静态变量。

Your Activity or Fragment Class:您的活动或片段类:

MainActivity Extends AppCompatActivity{

public static String myValue="";
//define this in your classs where you run the web service

}

your response method你的回应方式

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONObject jsonObject = response.getJSONObject("address");
                        String a = jsonObject.getString("extkey_next_receiving_address");

                     MainActivity.myValue=a;
                        Log.i("test", "onResponse: " + MainActivity.myValue);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("test2", "onErrorResponse: error");
        }
    });
    mQueue.add(request);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM