嗨我想從服務器獲取數據,當有人在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;
}
我不明白第二部分,你的意思是不正確的轉移價值?由於要搜索的id的內容取決於edittext的值,因此我認爲我應該將它留空。 – rekard 2014-11-02 09:12:41
你有params.add(新的BasicNameValuePair(「id」,「」));所以,在我看來,轉移到PHP的ID值是空的。你測試了這部分應用程序嗎? – MariuszT 2014-11-02 10:18:24
我改爲edittext字符串,edit = edt.getText()。toString(); 但是你怎麼表達te傳輸,因爲我不太清楚如何在java中傳遞變量$ id – rekard 2014-11-02 10:25:35