我有volley lib的问题,当我向服务器发布值时出现错误
org.json.JSONException: No value for taxi_list
所以当用户点击按钮时,它将不显示任何数据 这是代码:
// Creating volley request obj
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.d(TAG, s.toString());
try {
JSONObject jsonObject = new JSONObject(s);
if(!jsonObject.getBoolean("error")) {
JSONArray taxiJsonArray = jsonObject.getJSONArray("taxi_list");
// Parsing json
for (int i = 0; i < taxiJsonArray.length(); i++) {
JSONObject obj = taxiJsonArray.getJSONObject(i);
Taxi taxi = new Taxi();
taxi.settaxiname(obj.getString("taxiname"));
taxi.setThumbnailUrl(obj.getString("image"));
taxi.setdeparture(obj.getString("departure"));
taxi.setarrive(obj.getString("arrive"));
taxi.setseat(obj.getInt("seat"));
taxi.setcost(obj.getInt("cost"));
// adding taxi to taxi array
taxiList.add(taxi);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
//adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e(TAG, "Requesting Taxi Error: " + volleyError.getMessage());
Toast.makeText(getApplicationContext(),
volleyError.getMessage(), Toast.LENGTH_LONG).show();
}
})
这是我的json;
{
"error": false,
"taxi_list": [
{
"image": "http://localhost/androidapp/taxiprofile/1.jpg",
"taxiname": "Taxi 1",
"from": "PTK",
"to": "SGU",
"departure": "08:00:00",
"arrive": "13:00:00",
"seat": 7,
"cost": 12
},
{
"image": "http://localhost/androidapp/taxiprofile/default.jpg",
"taxiname": "Taxi 2",
"from": "PTK",
"to": "SGU",
"departure": "08:00:00",
"arrive": "13:00:00",
"seat": 2,
"cost": 15
},
{
"image": "http://localhost/androidapp/taxiprofile/2.jpg",
"taxiname": "Taxi Untung Selalu",
"from": "PTK",
"to": "SGU",
"departure": "09:00:00",
"arrive": "14:00:00",
"seat": 3,
"cost": 13
}
]
}
我试图修改代码,但没有成功...... 这是logcat:
D/Searching: {"error":false}
W/System.err: org.json.JSONException: No value for taxi_list
W/System.err: at org.json.JSONObject.get(JSONObject.java:354)
W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:544)
W/System.err: at com.testing.activity.Searchtaxi$4.onResponse(Searchtaxi.java:177)
W/System.err: at com.testing.activity.Searchtaxi$4.onResponse(Searchtaxi.java:168)
W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
W/System.err: at android.os.Handler.handleCallback(Handler.java:615)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err: at android.os.Looper.loop(Looper.java:137)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4745)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
错误是指JSONArray taxiJsonArray = jsonObject.getJSONArray(&#34; taxi_list&#34;)行
答案 0 :(得分:2)
尝试
jsonObject.getString("error") == false
因为false不是字符串。
答案 1 :(得分:1)
if (jsonObject.getBoolean("error")==false)
false不是字符串
答案 2 :(得分:1)
public class Calculator {
private static final int BASE = 10;
private int _value;
private int _va;
private CalculatorUI _ui;
private Operator op;
private enum Operator {
ADD, SUB, MUL, DIV, NO_OPERATION
};
public Calculator(CalculatorUI calculatorUI) {
op = Operator.NO_OPERATION;
_ui = calculatorUI;
_value = 0;
}
public void digit_0_keyPressed() {
_value = _value * BASE + 0;
_ui.updateDisplay();
}
public void digit_1_keyPressed() {
_value = _value * BASE + 1;
_ui.updateDisplay();
}
public void digit_2_keyPressed() {
_value = _value * BASE + 2;
_ui.updateDisplay();
}
public void digit_3_keyPressed() {
_value = _value * BASE + 3;
_ui.updateDisplay();
}
public void digit_4_keyPressed() {
_value = _value * BASE + 4;
_ui.updateDisplay();
}
public void digit_5_keyPressed() {
_value = _value * BASE + 5;
_ui.updateDisplay();
}
public void digit_6_keyPressed() {
_value = _value * BASE + 6;
_ui.updateDisplay();
}
public void digit_7_keyPressed() {
_value = _value * BASE + 7;
_ui.updateDisplay();
}
public void digit_8_keyPressed() {
_value = _value * BASE + 8;
_ui.updateDisplay();
}
public void digit_9_keyPressed() {
_value = _value * BASE + 9;
_ui.updateDisplay();
}
public void clear_keyPressed() {
_value = 0;
_ui.updateDisplay();
}
public int getValue() {
return _value;
}
public void equal_keyPressed(String uimessage) {
if (Operator.ADD.equals(op)) {
_value += _va;
} else if (Operator.MUL.equals(op)) {
_value *= _va;
} else if (Operator.DIV.equals(op)) {
_value = _va / _value;
} else if (Operator.SUB.equals(op)) {
_value = _va - _value;
}
_va = 0;
op = Operator.NO_OPERATION;
_ui.updateDisplay();
}
public void add_keyPressed() {
if (!Operator.NO_OPERATION.equals(op)) {
equal_keyPressed();
} else {
_ui.updateDisplay();
}
_va = _value;
_value = 0;
op = Operator.ADD;
}
public void mul_keyPressed() {
if (!Operator.NO_OPERATION.equals(op)) {
equal_keyPressed();
} else {
_ui.updateDisplay();
}
_va = _value;
_value = 0;
op = Operator.MUL;
}
}
的值为error
,请尝试
boolean
答案 3 :(得分:1)
首先尝试检查key
,然后获取JsonArray
if (jsonObject.has("taxi_list")) {
JSONArray taxiJsonArray = jsonObject.getJSONArray("taxi_list");
} else {
//show any toast or dialog that taxi list not available.
}
答案 4 :(得分:0)
只需使用
std::cout << "Received request: [" << std::string(static_cast<char*>(request.data()), request.size()) << "]" << std::endl;