如您所知,如果查询不成功,mysql_query()
则会返回一个 php 请求。false
随着时间的推移,我养成了一种习惯(你应该告诉我这是否是一个坏习惯)来制作类似的东西
$res = mysql_query(...);
if($res == false){
echo json_encode("notOK");
}else{
$return = array();
// process query result into that array
echo json_encode($return);
}
通过执行类似的操作,响应的处理在 JavaScript 中运行良好
// (...)
success : function(response){
var res = JSON.parse(resonse);
if(res == "notOK"){
// error handling
}else{
// proceed
}
}
// (...)
但是,在 Android 中,这似乎不起作用。我处理服务器请求的标准流程是
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
Vector<JSONObject> vector = new Vector<JSONObject>();
try{
response = client.execute(get);
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream in = entity.getContent();
String result = convertStreamtoString(in);
JSONArray resultArray = new JSONArray(result);
int len = resultArray.length();
for(int i=0; i<len; i++){
vector.add(resultArray.getJSONObject(i));
}
in.close();
}
} catch (//...
这导致JSONException
读取
04-14 17:13:23.011: W/System.err(433): org.json.JSONException: Value true of type java.lang.String cannot be converted to JSONArray
04-14 17:13:23.022: W/System.err(433): at org.json.JSON.typeMismatch(JSON.java:107)
04-14 17:13:23.022: W/System.err(433): at org.json.JSONArray.<init>(JSONArray.java:91)
04-14 17:13:23.022: W/System.err(433): at org.json.JSONArray.<init>(JSONArray.java:103)
04-14 17:13:23.022: W/System.err(433): at imhotapp.schlettibusiness.rest.RestHandler.login(RestHandler.java:52)
04-14 17:13:23.022: W/System.err(433): at imhotapp.schlettibusiness.LoginActivity.tryLogin(LoginActivity.java:48)
04-14 17:13:23.030: W/System.err(433): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 17:13:23.030: W/System.err(433): at java.lang.reflect.Method.invoke(Method.java:507)
04-14 17:13:23.030: W/System.err(433): at android.view.View$1.onClick(View.java:2139)
04-14 17:13:23.030: W/System.err(433): at android.view.View.performClick(View.java:2485)
04-14 17:13:23.030: W/System.err(433): at android.view.View$PerformClick.run(View.java:9080)
04-14 17:13:23.030: W/System.err(433): at android.os.Handler.handleCallback(Handler.java:587)
04-14 17:13:23.030: W/System.err(433): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 17:13:23.030: W/System.err(433): at android.os.Looper.loop(Looper.java:123)
04-14 17:13:23.030: W/System.err(433): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-14 17:13:23.030: W/System.err(433): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 17:13:23.030: W/System.err(433): at java.lang.reflect.Method.invoke(Method.java:507)
04-14 17:13:23.030: W/System.err(433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-14 17:13:23.030: W/System.err(433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-14 17:13:23.030: W/System.err(433): at dalvik.system.NativeStart.main(Native Method)
有没有办法像在 Javascript 中那样处理?我在做一些根本错误的事情吗?
PS:我在这里找到了这个问题,但我不认为它是重复的,因为这里是关于检查一个对象是否甚至是“JSON-familiar”