2016-12-22 37 views
0
服務器

我沒有從服務器獲取我的列表視圖圖片...它給了我唯一的啓動圖像,但無法從服務器的圖像...錯誤將圖像輸入列表視圖從使用JSON

這裏是我的子活動:

public class SubMenu extends Activity { 
    // Declare Variables 
    JSONObject jsonobject; 
    JSONArray jsonarray; 
    ListView listview; 
    ListViewAdapter adapter; 
    ProgressDialog mProgressDialog; 
    ArrayList<HashMap<String, String>> arraylist; 
    static String RANK = "id"; 
    static String COUNTRY = "name"; 

    static String FLAG = "image"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Get the view from listview_main.xml 
     setContentView(R.layout.activity_sub_menu); 
     // Execute DownloadJSON AsyncTask 
     new DownloadJSON().execute(); 
    } 

    // DownloadJSON AsyncTask 
    private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

     // @Override 
     // protected void onPreExecute() { 
     // super.onPreExecute(); 
     // Create a progressdialog 
     // mProgressDialog = new ProgressDialog(SubMenu.this); 
     // Set progressdialog title 
     // mProgressDialog.setTitle("Categories of Main categories....."); 
     // Set progressdialog message 
     // mProgressDialog.setMessage("Loading..."); 
     // mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     // mProgressDialog.show(); 
     // } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // Create an array 
      arraylist = new ArrayList<HashMap<String, String>>(); 
      // Retrieve JSON Objects from the given URL address 
      jsonarray = JsonFunctions 
        .getJSONfromURL("http://cloud.granddubai.com/broccoli/menu_typeitem.php"); 

      try { 
       // Locate the array name in JSON 
//     jsonarray = jsonobject.getJSONArray("main_menu_items"); 


       for (int i = 0; i < jsonarray.length(); i++) { 
        HashMap<String, String> map = new HashMap<String, String>(); 

        jsonobject = jsonarray.getJSONObject(i); 
        // Retrive JSON Objects 
        map.put("id", jsonobject.getString("id")); 
        map.put("name", jsonobject.getString("name")); 

        map.put("image", jsonobject.getString("image")); 
        // Set the JSON Objects into the array 
        arraylist.add(map); 
       } 
      } catch (JSONException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      listview = (ListView) findViewById(R.id.list1); 
      // Pass the results into ListViewAdapter.java 
      adapter = new ListViewAdapter(SubMenu.this, arraylist); 
      // Set the adapter to the ListView 
      listview.setAdapter(adapter); 
      // Close the progressdialog 
      // mProgressDialog.dismiss(); 
     } 
    } 
} 

我是新來的android ...多一些事情少數圖像不可用table.is的原因???? 這裏是我的JSON:

[ 
    { 
    "id": "1", 
    "name": "Bianca Pizza", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Bianca.png" 
    }, 
    { 
    "id": "2", 
    "name": "Cheese Pizza", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Cheese.png" 
    }, 
    { 
    "id": "3", 
    "name": "Chicken BBQ Pizza:", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Chicken BBQ.png" 
    }, 
    { 
    "id": "4", 
    "name": "Deluxe Pizza", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Deluxe.png" 
    }, 
    { 
    "id": "5", 
    "name": "Greek Pizza", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Greek.png" 
    }, 
    { 
    "id": "6", 
    "name": "Hawaiian Pizza", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Hawaiian.png" 
    }, 
    { 
    "id": "7", 
    "name": "Meat Lovers Pizza", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Meat Lovers.png" 
    }, 
    { 
    "id": "8", 
    "name": "Pepperoni Pizza", 
    "image": "" 
    }, 
    { 
    "id": "9", 
    "name": "Shrimp Pizza", 
    "image": "" 
    }, 
    { 
    "id": "10", 
    "name": "Vegetable Pizza", 
    "image": "" 
    }, 
    { 
    "id": "11", 
    "name": "Original Italian Pizza", 
    "image": "" 
    }, 
    { 
    "id": "12", 
    "name": "Beef Strips Pasta", 
    "image": "" 
    }, 
    { 
    "id": "13", 
    "name": "Chicken Pasta", 
    "image": "" 
    }, 
    { 
    "id": "14", 
    "name": "Meat Balls Pasta", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Meat Lovers.png" 
    }, 
    { 
    "id": "15", 
    "name": "Sausage Pasta", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Sausage.png" 
    }, 
    { 
    "id": "16", 
    "name": "Shrimp Pasta", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Shrimp.png" 
    }, 
    { 
    "id": "17", 
    "name": "Vegetable Pasta", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Vegetable.png" 
    }, 
    { 
    "id": "18", 
    "name": "Pesto Pasta", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Pesto.png" 
    }, 
    { 
    "id": "19", 
    "name": "Chicken Lasagna", 
    "image": "" 
    }, 
    { 
    "id": "20", 
    "name": "Meat Lasagna", 
    "image": "" 
    }, 
    { 
    "id": "21", 
    "name": "Vegetable Lasagna", 
    "image": "" 
    }, 
    { 
    "id": "22", 
    "name": "Caesar Salad", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Caesar.png" 
    }, 
    { 
    "id": "23", 
    "name": "Chicken Caesar Salad", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Chicken Caesar.png" 
    }, 
    { 
    "id": "24", 
    "name": "Garden Salad", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Garden.png" 
    }, 
    { 
    "id": "25", 
    "name": "Greek Salad", 
    "image": "" 
    }, 
    { 
    "id": "26", 
    "name": "Pasta Salad", 
    "image": "http://cloud.granddubai.com/broccoli/uploads/Pasta Salad.png" 
    }, 
    { 
    "id": "27", 
    "name": "Sunny Side up ", 
    "image": "" 
    }, 
    { 
    "id": "28", 
    "name": "Omelette ", 
    "image": "" 
    }, 
    { 
    "id": "29", 
    "name": "Scrambled Eggs", 
    "image": "" 
    }, 
    { 
    "id": "30", 
    "name": "Scrambled Egg with toppings", 
    "image": "" 
    }, 
    { 
    "id": "31", 
    "name": "Coffee", 
    "image": "" 
    }, 
    { 
    "id": "32", 
    "name": "Fresh Juice", 
    "image": "" 
    }, 
    { 
    "id": "36", 
    "name": "Water 500ml", 
    "image": "" 
    }, 
    { 
    "id": "35", 
    "name": "Pepsi Can", 
    "image": "" 
    }, 
    { 
    "id": "37", 
    "name": "Soup", 
    "image": "" 
    }, 
    { 
    "id": "38", 
    "name": "Special Offer Family Pack", 
    "image": "" 
    } 
] 

這是我的PHP文件

<?php 
include ('config.php'); 


$id = $_GET['id']; 



$sql = mysqli_query($conn,"SELECT * FROM main_menu_items "); 

$arr = array(); 
$i=0; 
while($result = mysqli_fetch_array($sql)) 
{ 
$arr[$i]['id']= $result['id']; 
$arr[$i]['name']= $result['name']; 
$arr[$i]['image']=$result['image']; 
$i++; 
} 
echo json_encode($arr); 

?> 

這是我ImageLoader的:

public class ImageLoader { 

    MemoryCache memoryCache = new MemoryCache(); 
    FileCache fileCache; 
    private Map<ImageView, String> imageViews = Collections 
      .synchronizedMap(new WeakHashMap<ImageView, String>()); 
    ExecutorService executorService; 
    // Handler to display images in UI thread 
    Handler handler = new Handler(); 

    public ImageLoader(Context context) { 
     fileCache = new FileCache(context); 
     executorService = Executors.newFixedThreadPool(5); 
    } 

    final int stub_id = R.drawable.nicon1; 

    public void DisplayImage(String url, ImageView imageView) { 
     imageViews.put(imageView, url); 
     Bitmap bitmap = memoryCache.get(url); 
     if (bitmap != null) 
      imageView.setImageBitmap(bitmap); 
     else { 
      queuePhoto(url, imageView); 
      imageView.setImageResource(stub_id); 
     } 
    } 

    private void queuePhoto(String url, ImageView imageView) { 
     PhotoToLoad p = new PhotoToLoad(url, imageView); 
     executorService.submit(new PhotosLoader(p)); 
    } 

    private Bitmap getBitmap(String url) { 
     File f = fileCache.getFile(url); 

     Bitmap b = decodeFile(f); 
     if (b != null) 
      return b; 

     // Download Images from the Internet 
     try { 
      Bitmap bitmap = null; 
      URL imageUrl = new URL(url); 
      HttpURLConnection conn = (HttpURLConnection) imageUrl 
        .openConnection(); 
      conn.setConnectTimeout(30000); 
      conn.setReadTimeout(30000); 
      conn.setInstanceFollowRedirects(true); 
      InputStream is = conn.getInputStream(); 
      OutputStream os = new FileOutputStream(f); 
      Utils.CopyStream(is, os); 
      os.close(); 
      conn.disconnect(); 
      bitmap = decodeFile(f); 
      return bitmap; 
     } catch (Throwable ex) { 
      ex.printStackTrace(); 
      if (ex instanceof OutOfMemoryError) 
       memoryCache.clear(); 
      return null; 
     } 
    } 

    // Decodes image and scales it to reduce memory consumption 
    private Bitmap decodeFile(File f) { 
     try { 
      // Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      FileInputStream stream1 = new FileInputStream(f); 
      BitmapFactory.decodeStream(stream1, null, o); 
      stream1.close(); 

      // Find the correct scale value. It should be the power of 2. 
      // Recommended Size 512 
      final int REQUIRED_SIZE = 70; 
      int width_tmp = o.outWidth, height_tmp = o.outHeight; 
      int scale = 1; 
      while (true) { 
       if (width_tmp/2 < REQUIRED_SIZE 
         || height_tmp/2 < REQUIRED_SIZE) 
        break; 
       width_tmp /= 2; 
       height_tmp /= 2; 
       scale *= 2; 
      } 

      // Decode with inSampleSize 
      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      FileInputStream stream2 = new FileInputStream(f); 
      Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2); 
      stream2.close(); 
      return bitmap; 
     } catch (FileNotFoundException e) { 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    // Task for the queue 
    private class PhotoToLoad { 
     public String url; 
     public ImageView imageView; 

     public PhotoToLoad(String u, ImageView i) { 
      url = u; 
      imageView = i; 
     } 
    } 

    class PhotosLoader implements Runnable { 
     PhotoToLoad photoToLoad; 

     PhotosLoader(PhotoToLoad photoToLoad) { 
      this.photoToLoad = photoToLoad; 
     } 

     @Override 
     public void run() { 
      try { 
       if (imageViewReused(photoToLoad)) 
        return; 
       Bitmap bmp = getBitmap(photoToLoad.url); 
       memoryCache.put(photoToLoad.url, bmp); 
       if (imageViewReused(photoToLoad)) 
        return; 
       BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad); 
       handler.post(bd); 
      } catch (Throwable th) { 
       th.printStackTrace(); 
      } 
     } 
    } 

    boolean imageViewReused(PhotoToLoad photoToLoad) { 
     String tag = imageViews.get(photoToLoad.imageView); 
     if (tag == null || !tag.equals(photoToLoad.url)) 
      return true; 
     return false; 
    } 

    // Used to display bitmap in the UI thread 
    class BitmapDisplayer implements Runnable { 
     Bitmap bitmap; 
     PhotoToLoad photoToLoad; 

     public BitmapDisplayer(Bitmap b, PhotoToLoad p) { 
      bitmap = b; 
      photoToLoad = p; 
     } 

     public void run() { 
      if (imageViewReused(photoToLoad)) 
       return; 
      if (bitmap != null) 
       photoToLoad.imageView.setImageBitmap(bitmap); 
      else 
       photoToLoad.imageView.setImageResource(stub_id); 
     } 
    } 

    public void clearCache() { 
     memoryCache.clear(); 
     fileCache.clear(); 
    } 

} 

這裏是我的列表視圖適配器

public class ListViewAdapter extends BaseAdapter { 

    // Declare Variables 
    Context context; 
    LayoutInflater inflater; 
    ArrayList<HashMap<String, String>> data; 
    ImageLoader imageLoader; 
    HashMap<String, String> resultp = new HashMap<String, String>(); 

    public ListViewAdapter(Context context, 
          ArrayList<HashMap<String, String>> arraylist) { 
     this.context = context; 
     data = arraylist; 
     imageLoader = new ImageLoader(context); 
    } 

    @Override 
    public int getCount() { 
     return data.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // Declare Variables 
     TextView id; 
     TextView name; 
     TextView population; 
     ImageView image; 

     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View itemView = inflater.inflate(R.layout.list_item1, parent, false); 
     // Get the position 
     resultp = data.get(position); 

     // Locate the TextViews in listview_item.xml 
     id = (TextView) itemView.findViewById(R.id.idq); 
     name = (TextView) itemView.findViewById(R.id.type1); 


     // Locate the ImageView in listview_item.xml 
     image = (ImageView) itemView.findViewById(R.id.subimg); 

     // Capture position and set results to the TextViews 
     id.setText(resultp.get(SubMenu.RANK)); 
     name.setText(resultp.get(SubMenu.COUNTRY)); 

     // Capture position and set results to the ImageView 
     // Passes flag images URL into ImageLoader.class 
     imageLoader.DisplayImage(resultp.get(SubMenu.FLAG), image); 
     // Capture ListView item click 
     /**itemView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // Get the position 
       resultp = data.get(position); 
       Intent intent = new Intent(context, SingleItemView.class); 
       // Pass all data rank 
       intent.putExtra("rank", resultp.get(MainActivity.RANK)); 
       // Pass all data country 
       intent.putExtra("country", resultp.get(MainActivity.COUNTRY)); 
       // Pass all data population 
       intent.putExtra("population",resultp.get(MainActivity.POPULATION)); 
       // Pass all data flag 
       intent.putExtra("flag", resultp.get(MainActivity.FLAG)); 
       // Start SingleItemView Class 
       context.startActivity(intent); 

      } 
     });*/ 
     return itemView; 
    } 
} 
+1

'的JSONObject '爲空 –

+0

那麼我會用什麼取代? –

+0

你能分享你的getJSONfromURL代碼嗎?你必須返回JSONArray。 –

回答

0

你需要這樣使用:

JSONArray response = JsonFunctions 
        .getJSONfromURL("http://cloud.gr....../broccoli/menu_typeitem.php"); 

因爲來自服務器的響應結構是JSONArray([])而不是JSONObject({})。這是起始因素是[]不是{}。

以下方法JsonFunctions類添加這一點,並調用這個方法:

public static JSONArray getJSONfromURL(String url) { 
     InputStream is = null; 
     String result = ""; 
     JSONArray jArray = null; 

     // Download JSON data from URL 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

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

     // Convert response to string 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 
     } catch (Exception e) { 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

     try { 

      jArray = new JSONArray(result); 
     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 

     return jArray; 
    } 

我改變你的代碼,請使用它:

public class SubMenu extends Activity { 
     // Declare Variables 
     JSONObject jsonobject; 
     JSONArray jsonarray; 
     ListView listview; 
     ListViewAdapter adapter; 
     ProgressDialog mProgressDialog; 
     ArrayList<HashMap<String, String>> arraylist; 
     static String RANK = "id"; 
     static String COUNTRY = "name"; 

     static String FLAG = "image"; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      // Get the view from listview_main.xml 
      setContentView(R.layout.activity_sub_menu); 
      // Execute DownloadJSON AsyncTask 
      new DownloadJSON().execute(); 
     } 

     // DownloadJSON AsyncTask 
     private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

      // @Override 
      // protected void onPreExecute() { 
      // super.onPreExecute(); 
      // Create a progressdialog 
      // mProgressDialog = new ProgressDialog(SubMenu.this); 
      // Set progressdialog title 
      // mProgressDialog.setTitle("Categories of Main categories....."); 
      // Set progressdialog message 
      // mProgressDialog.setMessage("Loading..."); 
      // mProgressDialog.setIndeterminate(false); 
      // Show progressdialog 
      // mProgressDialog.show(); 
      // } 

      @Override 
      protected Void doInBackground(Void... params) { 
       // Create an array 
       arraylist = new ArrayList<HashMap<String, String>>(); 
       // Retrieve JSON Objects from the given URL address 
       jsonarray = JsonFunctions 
         .getJSONfromURL("http://cloud.gr....../broccoli/menu_typeitem.php"); 

       try { 
        // Locate the array name in JSON 
//     jsonarray = jsonobject.getJSONArray("main_menu_items"); 


        for (int i = 0; i < jsonarray.length(); i++) { 
         HashMap<String, String> map = new HashMap<String, String>(); 

         jsonobject = jsonarray.getJSONObject(i); 
         // Retrive JSON Objects 
         map.put("id", jsonobject.getString("id")); 
         map.put("name", jsonobject.getString("name")); 

         map.put("image", jsonobject.getString("image")); 
         // Set the JSON Objects into the array 
         arraylist.add(map); 
        } 
       } catch (JSONException e) { 
        Log.e("Error", e.getMessage()); 
        e.printStackTrace(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void args) { 
       // Locate the listview in listview_main.xml 
       listview = (ListView) findViewById(R.id.list1); 
       // Pass the results into ListViewAdapter.java 
       adapter = new ListViewAdapter(SubMenu.this, arraylist); 
       // Set the adapter to the ListView 
       listview.setAdapter(adapter); 
       // Close the progressdialog 
       mProgressDialog.dismiss(); 
      } 
     } 
    } 

由於沒有名爲「main_menu_items」鍵,我註釋掉了代碼。

+0

當我替換它..它給我紅線說不兼容任務需要org.json.JSONArray找到org.json.JSONObject –

+0

你需要在你的JsonFunctions類中編寫一個單獨的方法返回一個JSONArray對象 – Madhan

+0

發佈你的JsonFunctions類的源代碼 – Madhan

0

你得到響應,JSONArray所以你

JSONArray jsonArray = JsonFunctions.getJSONfromURL("http://cloud.gr....../broccoli/menu_typeitem.php"); 

現在您可以訪問jsonArray,如:

for (int i = 0; i < jsonarray.length(); i++) { 
    HashMap<String, String> map = new HashMap<String, String>(); 

    jsonobject = jsonarray.getJSONObject(i); 
    // Retrive JSON Objects 
    map.put("id", jsonobject.getString("id")); 
    map.put("name", jsonobject.getString("name")); 
    map.put("image", jsonobject.getString("image")); 
    // Set the JSON Objects into the array 
    arraylist.add(map); 
} 

或許它會幫助你。謝謝。

+0

it把上面的答案放到我的代碼中,但它顯示了相同的紅線,並給出了相同的消息,即不兼容的任務需要org.json.JSONArray找到org.json。JSONObject - –

+0

你可以發佈你的json嗎? –

+0

複製你的json並把它放在http://json.parser.online.fr/beta/上,你可以看到你的json是以JSONArray開頭的。 –

0

//獲取數組

for (int i = 0; i < jsonarray.length(); i++) { 
    try { 
     JSONObject jsonobject = jsonarray.getJSONObject(i); 
     enter other parsing code here.. 
     }catch (JSONException e) { 
     e.printStackTrace(); 
     } 
} 

您可以使用此代碼段用於解析

+0

當我嘗試使用第一行它說不兼容任務所需組織.json.JSONArray發現org.json.JSONObject –

+0

如果我把如何保存url在jsonStr?即時獲取相同的紅線 –

+0

你在**結果**字符串中得到什麼? – Kunal

0

你不得不在兩個階級的JSONObject和JSONArray之間的衝突。 在這裏你走!

SubMenu.class

 // Declare Variables 
     // JSONObject jsonarray; 
     JSONArray jsonarray; 
     ListView listview; 
     //ListViewAdapter adapter; 
     ProgressDialog mProgressDialog; 
     ArrayList<HashMap<String, String>> arraylist; 
     static String RANK = "id"; 
     static String COUNTRY = "name"; 

     static String FLAG = "image"; 


     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      // Get the view from listview_main.xml 
      setContentView(R.layout.solver_activity); 
      // Execute DownloadJSON AsyncTask 
      new DownloadJSON().execute(); 
     } 

     // DownloadJSON AsyncTask 
     private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

      // @Override 
      // protected void onPreExecute() { 
      // super.onPreExecute(); 
      // Create a progressdialog 
      // mProgressDialog = new ProgressDialog(SubMenu.this); 
      // Set progressdialog title 
      // mProgressDialog.setTitle("Categories of Main categories....."); 
      // Set progressdialog message 
      // mProgressDialog.setMessage("Loading..."); 
      // mProgressDialog.setIndeterminate(false); 
      // Show progressdialog 
      // mProgressDialog.show(); 
      // } 

      @Override 
      protected Void doInBackground(Void... params) { 
       // Create an array 
       arraylist = new ArrayList<HashMap<String, String>>(); 
       // Retrieve JSON Objects from the given URL address 
       jsonarray = JsonFunctions 
         .getJSONfromURL("http://cloud.granddubai.com/broccoli/menu_typeitem.php"); 

       try { 
        for (int i = 0; i < jsonarray.length(); i++) { 
         HashMap<String, String> map = new HashMap<String, String>(); 
         JSONObject jsonobject; 
         jsonobject = jsonarray.getJSONObject(i); 
         // Retrive JSON Objects 
         map.put("id", jsonobject.getString("id")); 
         map.put("name", jsonobject.getString("name")); 

         map.put("image", jsonobject.getString("image")); 
         // Set the JSON Objects into the array 
         arraylist.add(map); 
         Log.e(TAG, "doInBackground: "+ i); 
        } 
       } catch (JSONException e) { 
        Log.e("Error", e.getMessage()); 
        e.printStackTrace(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void args) { 
       // Locate the listview in listview_main.xml 
       listview = (ListView) findViewById(R.id.list1); 
       // Pass the results into ListViewAdapter.java 
       adapter = new ListViewAdapter(SubMenu.this, arraylist); 
       // Set the adapter to the ListView 
       listview.setAdapter(adapter); 
       // Close the progressdialog 
       mProgressDialog.dismiss(); 
      } 
     } 

JsonFunction。類

public class JsonFunctions { 

    public static JSONArray getJSONfromURL(String url) { 
     InputStream is = null; 
     String result = ""; 
     JSONArray jArray = null; 

     // Download JSON data from URL 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

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

     // Convert response to string 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 
     } catch (Exception e) { 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

     try { 

      jArray = new JSONArray(result); 
     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 

     return jArray; 
    } 
} 
+0

什麼是在TAG中存儲和它的說法不能解決適配器 –

+0

它仍然給我錯誤 –

+0

PLZ再次看到我的問題 –

0

你可以這樣做

ArrayList<HashMap<String, String>> arraylist; 
private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

    @Override 
     protected Void doInBackground(Void... params) { 

      try { 
       arraylist = new ArrayList<HashMap<String, String>>(); 
       HttpHandler sh = new HttpHandler(); 

       // Making a request to url and getting response 
       String jsonStr = sh.makeServiceCall("http://cloud.granddubai.com/broccoli/menu_typeitem.php"); 

       Log.e(TAG, "JsonString = " + jsonStr); 

       JSONArray jsonObject = new JSONArray(jsonStr); 

       for (int i = 0; i< jsonObject.length(); i++){ 
        JSONObject job = jsonObject.getJSONObject(i); 
        Log.i(TAG, "Id = "+ job.getString("id")); 

       HashMap<String, String> map = new HashMap<String, String>(); 

       // Retrive JSON Objects 
       map.put("id", job.getString("id")); 
       map.put("name", job.getString("name")); 

       map.put("image", job.getString("image")); 
       // Set the JSON Objects into the array 
       arraylist.add(map); 

       } 

      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      Log.e("Downloaded DataSize = ", ""+arraylist.size()); 
     } 
    } 

這是你的HttpHandler

public class HttpHandler 
{ 
    private static final String TAG = HttpHandler.class.getSimpleName(); 

    public HttpHandler() { 
    } 

    public String makeServiceCall(String reqUrl) { 
     String response = null; 
     try { 
      URL url = new URL(reqUrl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestMethod("GET"); 
      // read the response 
      InputStream in = new BufferedInputStream(conn.getInputStream()); 
      response = convertStreamToString(in); 
     } catch (MalformedURLException e) { 
      Log.e(TAG, "MalformedURLException: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.e(TAG, "IOException: " + e.getMessage()); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.getMessage()); 
     } 
     return response; 
    } 

    private String convertStreamToString(InputStream is) { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line).append('\n'); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 
} 

當你想顯示的ImageView您的圖像,然後使用滑翔庫。
添加下面一行到你的應用程序級build.gradle文件

compile 'com.github.bumptech.glide:glide:3.7.0' 

現在你可以做這樣的代碼來下載圖像,並顯示到ImageView的

ImageView imageView = (ImageView) findViewById(R.id.my_image_view); 
Glide.with(this).load(url).into(imageView); 

。詳細請this link

+0

'TAG'只是'String'值,無論你想用什麼你都可以使用它。在我的案例'字符串TAG =「[ClassName]」 – Shailesh

+0

PLZ再次看到我的問題 –

+0

無需使用'ImageLoader'。使用'Glide'或'Picasso'庫從服務器下載圖像。 – Shailesh