0
I'm檢索從外部數據庫值到一個Android應用程序做的是這樣的服務器上的服務的調用檢索。這裏的php文件:檢查空值從外部服務
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$mysqli) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . sqli_connect_error());
}
$q=mysqli_query($mysqli, "SELECT title FROM post WHERE UPPER(title) LIKE UPPER('%" . $_REQUEST['searchThis'] . "%')");
while($e=mysqli_fetch_assoc($q)){
$output[]=$e;
}
print(json_encode($output));
mysqli_close();
這是我的班做什麼(我只是寫的事項的代碼):
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("CONNECTION_ERROR", "Error in http connection "+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
///-------------SYSTEM OUTS---------------
System.out.println(result);
System.out.println(result.getClass().getName());
}
catch(Exception e){
Log.e("BUFFER_ERROR", "Error converting result "+e.toString());
}
//------------------PROBLEMS---------------
if(result.equalsIgnoreCase("null")){
list.add("empty");
}
else{
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
//Log.i("result",json_data.getString("title"));
String title = json_data.getString("title");
list.add(title);
}
}
catch(JSONException e){
Log.e("DATA_PARSING_ERROR", "Error parsing data "+e.toString());
}
}
return list;
}
我試過白衣日誌和打印和就是我得到的是:
null //FOR System.out.println(result);
java.lang.String //for System.out.println(result.getClass().getName());
在服務器上的PHP服務返回的東西它的工作原理,但問題是,當我將結果與「null」進行比較,即使結果是值爲「null」的字符串,也始終爲false。當然,我遇到了JSON異常,因爲結果不是有效的轉換格式。這裏可能有什麼錯誤?
最愚蠢的mystake永遠...謝謝。 – 2013-02-14 15:26:18