2014-01-18 87 views
0

我在我的代碼中有一個錯誤,當我試圖顯示一個圖像到android的imageview從PHP的MySQL ..我使用JSON解析圖像..從PHP MySQL顯示圖像到安卓使用Json的imageview

因此,我已命名的影片

movie 
id_movie | Tittle | link_poster 
------------------------------------------------------------------- 
MP01  | Frozen |http://10.0.2.2/cinemainfo/image/movie1.jpg 

我將圖像保存到一個名爲cinemainfo中的htdocs /圖像文件夾..表..

所以這裏是我的細節.php代碼:

$response = array(); 

$id_movie = $_REQUEST['id_movie']; 

$sql="select tittle, link_poster from movie where id_movie = '".$id_movie."'"; 

$result = mysql_query($sql); 

if (mysql_num_rows($result) > 0) 
     { 
    $response["detail"] = array(); 

    while ($row = mysql_fetch_array($result)) 
     { 
     $detail = array(); 
     $detail["tittle"] = stripslashes($row["tittle"]); 
     $detail["link_poster"] = base64_encode($row["link_poster"]); 

     array_push($response["detail"], $detail); 
     } 

    $response["success"] = 1; 

    echo json_encode($response); 
     } 
     else { 

     $response["success"] = 0; 
     $response["message"] = "No data"; 

     echo json_encode($response); 
     } 

這裏是我detail.java代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.detail); 

    Bundle b = this.getIntent().getExtras(); 
    kode = b.getString("kode_intent"); 

    tittle=(TextView)findViewById(R.id.tv_tittle); 
    poster=(ImageView)findViewById(R.id.img_poster); 

    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
    postParameters.add(new BasicNameValuePair("id_movie", kode)); 

    String response = null; 

    try { 
     response = CustomHttpClient.executeHttpPost("http://10.0.2.2/cinemainfo/detail.php", postParameters); 
     String result = response.toString(); 

      try { 
       JSONArray jArray = new JSONArray(result); 
       JSONObject json_data=null; 
       for(int i=0;i<jArray.length();i++){ 

        json_data = jArray.getJSONObject(i); 

        ttl =json_data.getString("tittle"); 

        pstr = json_data.getString("link_poster"); 
       } 

       byte[] rawImage = Base64.decode(pstr, Base64.DEFAULT); 
       bmp = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length); 

      } 

      catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
       } 

      try { 
       tittle.setText(ttl); 
       poster.setImageBitmap(bmp); 
       } 

      catch(Exception e){ 
       Log.e("log_tag","Error in Display!" + e.toString());;   
       } 
     } 

     catch(Exception e){ 
      Log.e("log_tag", "Error in http connection"+e.toString()); 
     } 
} 
    } 

而且這裏是日誌貓:

E/log_tag(338): Error parsing data org.json.JSONException: Value {"success":1,"detail":[{"link_poster":"aHR0cDovLzEwLjAuMi4yL2NpbmVtYWluZm8vaW1hZ2UvZmlsbTEuanBn","tittle":"Frozen"}]} of type org.json.JSONObject cannot be converted to JSONArray 

當我運行這個網站:"http://10.0.2.2/cinemainfo/detail.php"我想確保我的PHP代碼是fine..and是.. 這裏是我的PHP代碼的結果:

{"detail":[ 
    {"tittle":"Frozen", 
    "link_poster":"aHR0cDovLzEwLjAuMi4yL2NpbmVtYWluZm8vaW1hZ2UvZmlsbTEuanBn" 
    } 
      ],"success":1 
} 

有誰知道如何解決我的問題?我被卡住來解決這個問題..任何幫助將是非常有幫助 謝謝:)

回答

0

你有json解析錯誤嘗試此代碼。

ArrayList<String> title= new ArrayList<String>(); 
     ArrayList<String> imgurl=new ArrayList<String>(); 
     String status=""; 

     try { 

JSONObject result=new JSONObject(jsonouput); 
status = result.getJSONObject("success"); 
JSONArray jArray = new JSONArray(result.getString("detail")); 

for(int i=0;i<jArray.length();i++){ 

json_data = jArray.getJSONObject(i).toString(); 

title.add(json_data.getString("tittle")); 

imgurl.add(json_data.getString("link_poster")); 

      } 
+0

我按照你的代碼,但'json_data'得到一個錯誤..錯誤是'json_data無法解析爲變量':( – Aprilia