简体   繁体   English

如何将 JSON 转换为 ARRAY 响应 GET 从 api Request In Android Java

[英]How can I Convert JSON to ARRAY Response GET from api Request In Android Java

I am tring to fetch data and but its show nothing.我正在尝试获取数据,但它什么也没显示。 even when I am trying to print on console same things happend即使我试图在控制台上打印同样的事情发生

 JsonObjectRequest jsonRequest = new JsonObjectRequest
            (Request.Method.GET, onewayUrl, null, response -> {


                try {
                    JSONArray jsonArray = new JSONArray(response);

                    for (int i = 0; i < jsonArray.length(); i++) {

                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String Career = jsonObject.getString("career");

                        textView = findViewById(R.id.text);
                        textView.setText(Career);

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


            }, error -> {
                error.printStackTrace();
            });

    Volley.newRequestQueue(AfterSearch.this).add(jsonRequest);
    jsonRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));

I got Some error looks like.我有一些错误看起来像。

W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:169)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:182)
W/System.err:     at 
com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:98)  

Usage of Retrofit is pretty more easy Retrofit 的使用非常简单

build.gradle

implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

ApiService.java

public interface ApiService {
  @GET("AirSearch/oneway.php?tripType=oneway&journeyfrom=DAC&journeyto=DXB&departuredate=2022-10-30&adult=1&child=0&infant=0")
  Call<List<Item>> getData();
}

ApiUtil.java

public final class ApiUtil {
    private static Retrofit retrofit;

    private static Retrofit provideRetrofit() {
        if(retrofit != null) {
            return retrofit;
        }
        retrofit = new Retrofit.Builder()
                .baseUrl("https://api.flyfarint.com/v.1.0.0/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit;
    }

    public static ApiService getApi() {
        return provideRetrofit().create(ApiService.class);
    }
}

MainActivity.java

private void fetchData() {
    ApiUtil.getApi().getData().enqueue(new Callback<List<Item>>() {
            @Override
            public void onResponse(@NonNull Call<List<Item>> call, @NonNull Response<List<Item>> response) {
                if(response.isSuccessful() && response.body() != null) {
+                   List<Item> data = response.body();
                    // data contains parsed json
                }
            }

            @Override
            public void onFailure(@NonNull Call<List<Item>> call, @NonNull Throwable t) {
                Log.e("ApiUtil", t.toString());
            }
        });
}

Item.java

public class Item {
    
    @SerializedName("system")
    @Expose
    public String system;
    @SerializedName("segment")
    @Expose
    public String segment;
    @SerializedName("career")
    @Expose
    public String career;
    @SerializedName("careerName")
    @Expose
    public String careerName;
    @SerializedName("BasePrice")
    @Expose
    public String basePrice;
    @SerializedName("Taxes")
    @Expose
    public String taxes;
    @SerializedName("price")
    @Expose
    public String price;
    @SerializedName("clientPrice")
    @Expose
    public String clientPrice;
    @SerializedName("departure")
    @Expose
    public String departure;
    @SerializedName("departureTime")
    @Expose
    public String departureTime;
    @SerializedName("departureDate")
    @Expose
    public String departureDate;
    @SerializedName("arrival")
    @Expose
    public String arrival;
    @SerializedName("arrivalTime")
    @Expose
    public String arrivalTime;
    @SerializedName("arrivalDate")
    @Expose
    public String arrivalDate;
    @SerializedName("flightduration")
    @Expose
    public String flightduration;
    @SerializedName("bags")
    @Expose
    public String bags;
    @SerializedName("seat")
    @Expose
    public String seat;
    @SerializedName("class")
    @Expose
    public String _class;
    @SerializedName("refundable")
    @Expose
    public String refundable;
    @SerializedName("segments")
    @Expose
    public List<Segment> segments = null;
    @SerializedName("transit")
    @Expose
    public Transit transit;
}

Segment.java

public class Segment  {
    @SerializedName("marketingcareer")
    @Expose
    public String marketingcareer;
    
    @SerializedName("marketingcareerName")
    @Expose
    public String marketingcareerName;
    
    @SerializedName("marketingflight")
    @Expose
    public String marketingflight;
    @SerializedName("operatingcareer")
    @Expose
    public String operatingcareer;
    @SerializedName("operatingflight")
    @Expose
    public String operatingflight;
    @SerializedName("departure")
    @Expose
    public String departure;
    @SerializedName("departureAirport")
    @Expose
    public String departureAirport;
    @SerializedName("departureLocation")
    @Expose
    public String departureLocation;
    @SerializedName("departureTime")
    @Expose
    public Date departureTime;

    @SerializedName("arrival")
    @Expose
    public String arrival;

    @SerializedName("arrivalTime")
    @Expose
    public Date arrivalTime;

    @SerializedName("arrivalAirport")
    @Expose
    public String arrivalAirport;
    @SerializedName("arrivalLocation")
    @Expose
    public String arrivalLocation;
    @SerializedName("flightduration")
    @Expose
    public String flightduration;
    @SerializedName("bookingcode")
    @Expose
    public String bookingcode;
    @SerializedName("seat")
    @Expose
    public String seat;
}

Transit.java

public class Transit {

    public final static long serialVersionUID = 5413373419576714522L;
    @SerializedName("transit1")
    @Expose
    public String transit1;
}
 String url = "https://api.flyfarint.com/v.1.0.0/AirSearch/oneway.php?tripType=oneway&journeyfrom=DAC&journeyto=DXB&departuredate=2022-10-30&adult=1&child=0&infant=0";
    private ProgressDialog pDialog;
   pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, response -> {
            hidePDialog();
            for (int i = 0; i < response.length(); i++) {

                try {
                    JSONObject responseObj = response.getJSONObject(0); //showing here only one using recyclerview you can show all.USE "i" for all records with model.
                    String careerName = responseObj.getString("careerName");
                    TextView textView=findViewById(R.id.text);
                    textView.setText(careerName);

                } catch (JSONException e) {
                    e.printStackTrace();
                    hidePDialog();
                }
            }
        }, error -> hidePDialog());
        jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
        queue.add(jsonArrayRequest);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

暂无
暂无

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

相关问题 如何将数组转换为 Json (Java) - How can I convert an Array to Json (Java) 我如何发出HTTP POST请求并使用我应该在android中获得的Json响应 - How can i make an HTTP POST request and and use the Json Response i should get in android 如何在json响应中获取数组的大小? 放心API、JAVA - How get size of an array in json response? rest assured API, JAVA 为JSON GET请求创建Web API,如何为该模式创建JSON对象/数组 - creating a web API for JSON GET Request, how can i create JSON Objects/array for this Pattern 如何在Java集合中转换为json响应的字符串? - how can i convert this string which is json response in java collection? 如何从android(java)中的json响应中获取内部索引对象,该对象对rest api调用使用改造 - How to get inner indexed object from json response in android (java) which use retrofit for rest api calls 如何从服务器获取数据作为json数组并将其转换为Java数组以在android应用程序中使用 - How to get data as an json array from a server and convert it in to java array to use in android application 如何从Java发送压缩(gzip)JSON作为对Ajax请求的响应? - How can I send compressed (gzip) JSON as response to an Ajax Request, from Java? Java Servlet如何从Shopify API获取json响应 - Java Servlet how to get the json response from Shopify API 如何在 Java 中发布请求以获取 JSON 中的响应 - How to post request in java to get the response in JSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM