简体   繁体   中英

Java Json Request Android

public class BtcPaymentQR extends 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. 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.

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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