我可以选择并显示数据库中的所有图像。现在我想通过id过滤它们。如何通过id从db获取图像?
有没有办法比较AsyncTask中的字符串“imovel_id”和从db中选择的“imovel_id”json响应?
或者通过Android应用程序发送的请求获取字符串“imovel_id”然后通过“imovel_id”选择所有图像会更好吗?
我尝试在下面的代码中从AsyncTask发送字符串,在php请求上获取它,然后使用WHERE clausule的查询,但我没有成功。我得到一个空值。
include_once 'db_connect.php';
$i_id = $_REQUEST["imovel_id"];
$sql = "SELECT * FROM iMoveis WHERE imovel_id='$i_id'";
$result = mysqli_query($mysqli, $sql);
$response = array();
$images = array();
while($row = mysqli_fetch_assoc($result)){
$images[] = array('images' => $row['img1']);
$images[] = array('images' => $row['img2']);
$images[] = array('images' => $row['img3']);
$images[] = array('images' => $row['img4']);
$images[] = array('images' => $row['img5']);
}
$response['posts'] = $images;
echo json_encode($response, JSON_UNESCAPED_SLASHES);
php脚本
{{1}}
在json数组中添加id然后在parseResult方法中将其与textview中的字符串“imovel_id”进行比较可能是一个解决方案吗?
我认为通过nameValuePairs发送字符串会很容易,因为我已经完成并在查询中使用WHERE进行过滤,但似乎这样做不那么容易。
提前致谢。
答案 0 :(得分:0)
我在AsyncTask中做了一些更改
public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
@Override
protected Integer doInBackground(String... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("imovel_id", i_id));
Integer result = 0;
try {
// Create Apache HttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://meuwebsite.com/panel/json_images.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpclient.execute(httppost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
// 200 represents HTTP OK
if (statusCode == 200) {
String response = streamToString(httpResponse.getEntity().getContent());
parseResult(response);
result = 1; // Successful
} else {
result = 0; //"Failed
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
// Download complete. Lets update UI
if (result == 1) {
mGridAdapter.setGridData(mGridData);
} else {
Toast.makeText(GridViewActivity.this, "Failed to fetch data!", Toast.LENGTH_SHORT).show();
}
//Hide progressbar
mProgressBar.setVisibility(View.GONE);
}
}
String streamToString(InputStream stream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
String line;
String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
// Close stream
if (null != stream) {
stream.close();
}
return result;
}
/**
* Parsing the feed results and get the list
*
* @param result
*/
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("posts");
GridItem item;
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
item = new GridItem();
item.setImage(post.getString("images"));
mGridData.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
还将$i_id = $_REQUEST["imovel_id"];
更改为$i_id = $_POST["imovel_id"];