Not Getting data from server

北城以北 提交于 2020-01-17 05:31:52

问题


I am trying to get data from server, I have done all the coding part I am getting status code is 200 (success) but not getting data.

This is my JSON data:

{
  "status": 200,
  "data": [
    {
      "id": "1",
      "company_id": "8",
      "customer_id": "17",
      "driver_id": null,
      "city_id": "2",
      "vehicletype_id": "4",
      "status": "Pending",
      "source_long": "77.59505250000007",
      "source_lat": "12.9998698",
      "dest_long": "77.62877609999998",
      "dest_lat": "12.946679",
      "source": "Jayamahal, Bengaluru, Karnataka, India",
      "destination": "Ejipura Bus Stand, Ejipura Main Road, Ejipura, Bengaluru, Karnataka, India",
      "bookingtime": "2016-06-14 06:55:38"
    }
  ]
}

This is my code to get data:

 JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, OPEN_BOOKINGS_URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String string = null;
                JSONArray ja =null;
                try {
                    Toast.makeText(getContext(), "You are in Try Block", Toast.LENGTH_SHORT).show();
                    ja=response.getJSONArray("data");
                    if(ja!=null)
                    {
                        Toast.makeText(getContext(), "Ja is not null with data", Toast.LENGTH_SHORT).show();
                       for (int i=0; i<ja.length(); i++)
                       {
                           Toast.makeText(getActivity(), "for loop", Toast.LENGTH_SHORT).show();
                           JSONObject jo = ja.getJSONObject(i);
                           Toast.makeText(getActivity(), jo.toString(), Toast.LENGTH_SHORT).show();
                       }
                    }
                    else
                    {
                        tv.setText("ja is null");
                       // Toast.makeText(getActivity(), "ja is null", Toast.LENGTH_SHORT).show();
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Toast.makeText(getActivity(), response.toString(), Toast.LENGTH_SHORT).show();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                tv.setText(error.toString());
            }
        });
        rq = Volley.newRequestQueue(getContext());
        rq.add(jor);
    }

In the code I have written toast to check whether my code block is executing or not.

I am getting data like,

{
  "status": 200,
  "data": [
    {}
  ]
}

回答1:


Please try to use the below code

 private void callDashboradWS() {
    AsyncTask<String, String, String> _Task = new AsyncTask<String, String, String>() {
        String _responseMain = "";


        @Override
        protected void onPreExecute() {
            Constant.showLoader(getActivity());

        }

        @Override
        protected String doInBackground(String... arg0) {
            if (NetworkAvailablity.checkNetworkStatus(getActivity())) {
                try {
                    HttpURLConnection urlConnection;
                    //  String query = builder.build().getEncodedQuery();
                    URL url = new URL("YOURSERVICEURL));
                    System.out.println(url.toString() + " <<<");
                    urlConnection = (HttpURLConnection) ((url.openConnection()));
                    urlConnection.setDoInput(true);
                    urlConnection.setDoOutput(true);
                    urlConnection.setUseCaches(false);
                    urlConnection.setChunkedStreamingMode(1024);
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setReadTimeout(10000);
                    urlConnection.connect();


                    // Write
                    OutputStream outputStream = urlConnection.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                    //  writer.write(query);
                    writer.close();
                    outputStream.close();

                    System.out.println("Response code :-- " + urlConnection.getResponseCode());
                    int responsecode = urlConnection.getResponseCode();
                    //Read

                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
                    String line = null;
                    StringBuilder sb = new StringBuilder();
                    while ((line = bufferedReader.readLine()) != null) {
                        System.out.println("in while");
                        sb.append(line);
                    }
                    bufferedReader.close();
                    _responseMain = sb.toString();


                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        Constant.showToast("Server Error ", getActivity());
                    }
                });
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            Constant.hideLoader();
            System.out.println("SIGNIN RESPONSE: " + _responseMain);

        }


    };
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        _Task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (String[]) null);
    } else {
        _Task.execute((String[]) null);
    }
}



回答2:


You are not getting Data from server side, there is no issue by your side.. You should check to server side why not sending data.. you did well...



来源:https://stackoverflow.com/questions/37968302/not-getting-data-from-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!