请帮我在下面的代码中找到错误。
我已将Android文件中的值通过namevaluepair
发布到PHP文件。
我收到以下错误:
注意:未定义索引:城市{"结果":[]}
<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "sampledb";
$con = new mysqli($host, $user, $password, $dbname);
$city = $_POST['city'];
$con->query("SET NAMES 'utf8'");
$stmtsearch = "SELECT a.title, a.phone, a.description, a.email, a.post_date FROM `ad_master` a INNER JOIN tbl_city_master b ON a.city_id = b.fld_city_id WHERE b.fld_city_name = '$city'";
$resultsearch = $con->query($stmtsearch);
$json = array();
while ($row = mysqli_fetch_array($resultsearch)) {
array_push($json, array(
'title' => $row[0],
'phone' => $row[1],
'description' => $row[2],
'email' => $row[3],
'post_date' => $row[4]
));
}
echo json_encode(array("result" => $json));
$con->close();
?>
Android代码:
public void getData() {
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
InputStream inputStream = null;
String result = null;
Intent in = getIntent();
String city = in.getStringExtra(("city"));
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("city", city));
DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://10.0.2.2/qsearchdetail.php");
try {
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) inputStream.close();
} catch (Exception squish) {
}
return result;
}
}
@Override
protected void onPostExecute(String result) {
myJSON = result;
showList();
}
}
GetDataJSON g=new GetDataJSON();
g.execute();
}
public void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
search = jsonObj.getJSONArray(TAG_RESULT);
for (int i = 0; i < search.length(); i++) {
JSONObject c = search.getJSONObject(i);
String title = c.getString(TAG_TITLE);
String phone = c.getString(TAG_PHONE);
String email = c.getString(TAG_EMAIL);
String description = c.getString(TAG_DESCRIPTION);
String postDate = c.getString(TAG_DATE);
HashMap<String, String> search = new HashMap<String, String>();
search.put(TAG_TITLE, title);
search.put(TAG_PHONE, phone);
search.put(TAG_EMAIL, email);
search.put(TAG_DESCRIPTION, description);
search.put(TAG_DATE, postDate);
searchList.add(search);
}
ListAdapter adapter = new SimpleAdapter(
QResultDetail.this,searchList, R.layout.search_result,
new String[]{TAG_TITLE, TAG_PHONE, TAG_EMAIL, TAG_DESCRIPTION,TAG_DATE},
new int[] { R.id.tvTitle, R.id.tvMobile, R.id.tvEmail, R.id.tvDesp, R.id.tvDate }
);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
您没有发送nameValuePairs
。