I'm try to return JSONArray from other package.
But my log is 'D/test: []'.
how can I return JSONArray from other package?
I want to do
RemSeat r = new RemSeat();
JSONArray aj = r.getremseat(getApplicationContext(),"20220827", "0671801", "3112001");
Log.d("test", aj.toString());
in my MainActivity.
TerCode.java
package remseatcomponent;
import android.content.Context;
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 RemSeat {
public JSONArray getremseat (Context context, String timDte, String terFrI, String terToI) {
/*
String timDte = "20220827";
String terFrI = "0671801";
String terToI = "3112001";
*/
JSONArray ja = new JSONArray();
String url = "https://MY URL"; //myURL
String timTimI = "0000"; //
RequestQueue queue = Volley.newRequestQueue(context);
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;
}
}
MainActivity.java
package com.example.terapi3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import remseatcomponent.RemSeat;
import android.util.Log;
import org.json.JSONArray;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// I want to do this on MainActivity
RemSeat r = new RemSeat();
JSONArray aj = r.getremseat(getApplicationContext(),"20220827", "0671801", "3112001");
Log.d("test", aj.toString());
}
}
How can I returnJSONArray from other package's method using volley?