2014-12-03 44 views
1

您好我有其他解析JSON的代碼和JSON的base64圖像正常工作:如何解析從androidw¡

try{ 
     JSONArray json = jparser.GetJSONfromUrl(url); 
     for(int i =0;i <json.length();i++){ 
      try { 
       JSONObject c = json.getJSONObject(i); 
       String vpeli = c.getString(TAG_PELI); 
       String vname2 = c.getString(TAG_nombre2); 
      } catch (JSONException e) { 
       // TODO: handle exception 
       e.printStackTrace(); 
       return "error creando variables"; 
      } 

我要解析的json的base64圖像轉換成一個機器人的ImageView,我的JSON是:

<?php 

$con = mysql_connect('mysql.hostinger.es', 'u453215752_aitor', '*******'); 
mysql_query("SET CHARACTER SET utf8"); 
mysql_query("SET NAMES utf8"); 

$cities['cities'] = array(); 

if($con) 
{ 
mysql_select_db('u453215752_droid'); 

$res = mysql_query('select name, nametwo, photo from cities'); 

while($row = mysql_fetch_array($res)) { 
array_push($cities['cities'], array('name' => $row['name'], 'nametwo' => 
$row['nametwo'], 'photo' => base64_encode($row['photo']))); 
} 
mysql_free_result($res); 
mysql_close($con); 
} 
header('Content-type: application/json'); 
echo json_encode($cities); 

我需要在imageview中解析「照片」嗎? 謝謝!

回答

0

我希望我的答案是完整的,你只是想解碼一個JSON字符串的位圖,你已經有權利?

試試這個

private Bitmap decodeBitmap(String input) 
    { 
     byte[] byteArr = Base64.decode(input, 0); 
     return BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length); 
    }