我正在尝试从服务器获取数据,我已经完成了所有编码部分,我得到的状态代码是200(成功)但没有获取数据。
这是我的JSON数据:
{
"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"
}
]
}
这是我获取数据的代码:
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);
}
在代码中我写了吐司,检查我的代码块是否正在执行。
我正在获取数据
{
"status": 200,
"data": [
{}
]
}
答案 0 :(得分:0)
请尝试使用以下代码
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);
}
}
答案 1 :(得分:0)
您没有从服务器端获取数据,您的身边没有问题.. 您应该检查服务器端为什么不发送数据.. 你表现得很好......