2014-11-02 88 views
0

嗨我想從服務器獲取數據,當有人在edittext中輸入一個id時,如果有匹配的數據應該顯示在textviews中。通過edittext獲取選擇查詢SELECT

我得到它與一個靜態ID,所以可以說我有SELECT * FROM TABLE where id='1'號碼1如果號碼1被輸入在編輯文本中,號碼1的數據將顯示出來。

null通常顯示在我的瀏覽器中。

但現在,我使用$id=_REQUEST['id']的jsonparser只返回NPE(空指針異常),因爲jsonparser顯示錯誤:Error parsing data org.json.JSONException: Value nulln of type java.lang.String cannot be converted to JSONObject我想查詢的心不是解僱。

我要告訴你我的JSON解析器類和一塊我的PHP。

<?php 
$mysqli = new mysqli("host", "user", "password", "database"); 
if (mysqli_connect_errno()) { 
printf("Connect failed: %s\n", mysqli_connect_error()); 
exit(); 
} 
$id=$_REQUEST['id']; 
$result = $mysqli->query("Select * From Table where id = '$id'"); 
while($row = mysqli_fetch_assoc($result)) { 
$rows=$row; 
} 
echo json_encode($rows); 
$result->close(); 
$mysqli->close();?> 

JSON分析器類

public class JSONParser { 
    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 
String id; 
    private static final String TAG_ID = "id"; 

// constructor 
public JSONParser() { 
} 
public JSONObject getJSONFromUrl(String url) { 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("id", "")); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    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(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 
    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 
    // return JSON String 
    return jObj; 
} 

回答

1

首先,你必須在你的代碼的SQL注入漏洞。你不能在沒有驗證的情況下在你的sql查詢中使用任何變量!嘗試使用PDO http://php.net/manual/en/pdo.prepare.php

其次,你似乎沒有從Java到PHP的正確的ID傳輸值,你正在搜索ID =''的行。

第三,$ rows = $行將不起作用。添加$ rows = array();之前,然後使用$ rows [] = $ row ;.

+0

我不明白第二部分,你的意思是不正確的轉移價值?由於要搜索的id的內容取決於edittext的值,因此我認爲我應該將它留空。 – rekard 2014-11-02 09:12:41

+0

你有params.add(新的BasicNameValuePair(「id」,「」));所以,在我看來,轉移到PHP的ID值是空的。你測試了這部分應用程序嗎? – MariuszT 2014-11-02 10:18:24

+0

我改爲edittext字符串,edit = edt.getText()。toString(); 但是你怎麼表達te傳輸,因爲我不太清楚如何在java中傳遞變量$ id – rekard 2014-11-02 10:25:35