2015-11-04 124 views
1

我可以從db中選擇並顯示所有圖像。現在我打算通過id過濾它們。我如何通過ID從數據庫獲取圖像?Android + json通過編號獲取圖像

有沒有辦法從數據庫中選擇了「imovel_id」 JSON響應比較的AsyncTask裏面的字符串「imovel_id」?

或者是更好地得到通過請求字符串「imovel_id」從Android應用程式傳送,然後選擇通過「imovel_id」的所有圖像?

我想,在下面的代碼從發送的AsyncTask的字符串,得到它在PHP請求,然後使用具有WHERE clausule,但我沒有成功的查詢。我得到一個空值。

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)); 

     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://meuwebsite.com/panel/json_images.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      inputStream = entity.getContent(); 
      Log.e("pass 1", "connection success "); 
     } catch (Exception e) { 
      Log.e("Fail 1", e.toString()); 
      Toast.makeText(getApplicationContext(), "Invalid IP Address", 
        Toast.LENGTH_LONG).show(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader 
        (new InputStreamReader(inputStream, "UTF-8")); 
      StringBuilder sb = new StringBuilder(); 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      inputStream.close(); 
      result = sb.toString(); 
      Log.e("pass 2", "connection success "); 
     } catch (Exception e) { 
      Log.e("Fail 2", e.toString()); 
     } 

     try { 
      JSONObject json_data = new JSONObject(result); 
      i_id = (json_data.getString("imovel_id")); 
      Log.e("pass 1", "id do imovel = " + i_id); 
     } catch (Exception e) { 
      Log.e("Fail 3", e.toString()); 
     } 


     Integer result = 0; 
     try { 
      // Create Apache HttpClient 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0])); 
      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(); 
    } 
} 

PHP腳本

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); 

添加JSON數組內的ID,然後將其從所述的TextView比較parseResult方法與字符串「imovel_id」內可能是一個解決方案?

我想,我已經做了,並在查詢過濾器中使用WHERE它會很容易通過namevaluepairs中發送的字符串,但它似乎沒有that's工作那麼容易。

在此先感謝。

回答

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"];