簡體   English   中英

如何使用凌空從方法返回 JSONArray?

[英]how to return JSONArray from method using volley?

我正在嘗試在我的方法上返回JSONArray

我想看看

String test = getremseat("20220827", "0671801", "3112001").toString();`        
Log.i("test", test);'

但結果是I/test: []

我認為getremseat方法不會因為queue.add(req);而返回如何從使用 Volley 的方法返回JSONArray

MainActivity.java

package com.example.terapi3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;


import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity {

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

        String test = getremseat("20220827", "0671801", "3112001").toString();
        Log.i("test", test);
    }

    public JSONArray getremseat (String timDte, String terFrI, String terToI) {
     /*
        String timDte = "20220826";
        String terFrI = "0671801";
        String terToI = "3112001";
    */

        JSONArray ja = new JSONArray();

        // GET requset url
        String url = "https://apigw.tmoney.co.kr:5556/gateway/xzzIbtListGet/v1";
        String timTimI = "0000";  //

        RequestQueue queue = Volley.newRequestQueue(this);


        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url + "/ibt_list" + "/" + timDte + "/" + timTimI + "/" + terFrI + "/" + terToI + "/9/0",
                null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONArray jsonArray = response.getJSONObject("response").getJSONArray("LINE_LIST");


                    for (int a = 0; a < jsonArray.length(); a++) {
                        JSONObject jo = jsonArray.getJSONObject(a);

                        JSONObject joo = new JSONObject();
                        joo.put("TIM_TIM", jo.getString("TIM_TIM"));
                        joo.put("LIN_TIM", jo.getString("LIN_TIM"));
                        joo.put("REM_CNT", jo.getString("REM_CNT"));

                        ja.put(joo);

                    }
                    

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

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("e", "Site Info Error: " + error.getMessage());
            }
        }) {
            // header
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("x-Gateway-APIKey", "MY PERSONAL KEY");
                return headers;
            }


        };


        queue.add(req);
        return ja; // i want to return this
    }

}

而且,我還想將JSONArray返回給其他 package。

您得到一個空的 JSONArray,因為您在 API 給出響應之前登錄。

您可以通過在收到 API 響應后調用回調方法來解決此問題。

例子

創建

private String test = "";

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

    getremseat("20220827", "0671801", "3112001").toString();
}

getremseat

. . .

for (int a = 0; a < jsonArray.length(); a++) {
      JSONObject jo = jsonArray.getJSONObject(a);

      JSONObject joo = new JSONObject();
      joo.put("TIM_TIM", jo.getString("TIM_TIM"));
      joo.put("LIN_TIM", jo.getString("LIN_TIM"));
      joo.put("REM_CNT", jo.getString("REM_CNT"));
      ja.put(joo);
}
test = ja.toString(); <-----
onResult(ja);         <-----

. . .

onResult

public void onResult (JSONArray jsonArray){
    Log.d("tag", jsonArray.toString());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM