2013-11-24 2 views
0

Android에서 Json을 사용하여 데이터를 수집하는 맞춤 목록이 있습니다. 앱 목록을 올바르게로드하고 다른 목록을 닫습니다.Asynctask의 오류 목록보기

https://www.dropbox.com/s/yywp1aa6vlc66y4/error_list.png

그리고 내 코드는 다음과 같습니다 : 로그 캣의 오류는

public class Lista_productos extends Activity{ 

ListView mListView; 

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.lista_producto); 
    // URL to the JSON data  
    int section = 1; 

    String strUrl = "http://server/file.php?valor="+section; 

    // Creating a new non-ui thread task to download json data 
    DownloadTask downloadTask = new DownloadTask(); 

    // Starting the download process 
    downloadTask.execute(strUrl); 

    // Getting a reference to ListView of activity_main 
    mListView = (ListView) findViewById(R.id.listaJson); 

    Button Buttonback = (Button) findViewById(R.id.back); 

    Buttonback.setOnClickListener(new View.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      finish(); 
     } 

    }); 
} 


    /** A method to download json data from url */ 
    private String downloadUrl(String strUrl) throws IOException{ 

     String data = ""; 
     InputStream iStream = null; 
     try{ 
       URL url = new URL(strUrl); 

       // Creating an http connection to communicate with url 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

       // Connecting to url 
       urlConnection.connect(); 

       // Reading data from url 
       iStream = urlConnection.getInputStream(); 

       BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); 

       StringBuffer sb = new StringBuffer(); 

       String line = ""; 
       while((line = br.readLine()) != null){ 
        sb.append(line); 
       } 

       data = sb.toString(); 

       br.close(); 

     }catch(Exception e){ 
       Log.d("Exception while downloading url", e.toString()); 
     }finally{ 
       iStream.close(); 
     } 

     return data; 
    } 



    /** AsyncTask to download json data */ 
    private class DownloadTask extends AsyncTask<String, Integer, String>{ 
     String data = null; 
     private ProgressDialog dialog; 

     protected void onPreExecute() { 
      dialog = ProgressDialog.show(Lista_productos.this, "", "Cargando...", true); 

     } 

       @Override 
       protected String doInBackground(String... url) { 
         try{ 
          data = downloadUrl(url[0]); 

         }catch(Exception e){ 
          Log.d("Background Task",e.toString()); 
         } 
         return data; 
       } 

       @Override 
       protected void onPostExecute(String result) { 
         // The parsing of the xml data is done in a non-ui thread 
         ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask(); 
         // Start parsing xml data 
         listViewLoaderTask.execute(result); 
         dialog.dismiss(); 
       } 
    } 

    /** AsyncTask to parse json data and load ListView */ 
    private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{ 
     JSONObject jObject; 
     // Doing the parsing of xml data in a non-ui thread 
     @Override 
     protected SimpleAdapter doInBackground(String... strJson) { 
      try{ 
       jObject = new JSONObject(strJson[0]); 
       JSONParser DealJsonParser = new JSONParser(); 
       DealJsonParser.parse(jObject); 
      }catch(Exception e){ 
       Log.d("JSON Exception1",e.toString()); 
      } 

      // Instantiating json parser class 
      JSONParser DealJsonParser = new JSONParser(); 

      // A list object to store the parsed countries list 
      List<HashMap<String, Object>> deal = null; 

      try{ 
       // Getting the parsed data as a List construct 
       deal = DealJsonParser.parse(jObject); 
      }catch(Exception e){ 
       Log.d("Exception",e.toString()); 
      }   

      // Keys used in Hashmap 
      String[] from = { "flag","details","precionuevo","descripcion","total","codeqr","latitud","longitud","unidades","telefono"}; 

      // Ids of views in listview_layout 
      int[] to = { R.id.dealimage,R.id.details,R.id.price,R.id.descripcion,R.id.total,R.id.codeqr,R.id.latitud,R.id.longitud,R.id.unidades,R.id.telefono}; 

      // Instantiating an adapter to store each items 
      // R.layout.listview_layout defines the layout of each item 

      SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), deal, R.layout.list_view, from, to); 

      return adapter; 
     } 

     /** Invoked by the Android on "doInBackground" is executed */ 
     @Override 
     protected void onPostExecute(SimpleAdapter adapter) { 
      // Setting adapter for the listview   
      mListView.setAdapter(adapter); 

      for(int i=0;i<adapter.getCount();i++){ 
       HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i); 
       String imgUrl = (String) hm.get("flag_path"); 
       ImageLoaderTask imageLoaderTask = new ImageLoaderTask(); 

       HashMap<String, Object> hmDownload = new HashMap<String, Object>(); 
       hm.put("flag_path",imgUrl); 
       hm.put("position", i); 

       // Starting ImageLoaderTask to download and populate image in the listview 
       imageLoaderTask.execute(hm); 
      } 

      mListView.setOnItemClickListener(new OnItemClickListener() { 

       public void onItemClick(AdapterView<?> a, View view, int position, long id) { 
        // TODO Auto-generated method stub 
          String URL = ((TextView) view.findViewById(R.id.descripcion)).getText().toString();//this will take the value of the invincible textView 

          ImageView imageview = (ImageView) view.findViewById(R.id.dealimage); 
          imageview.buildDrawingCache(); 
          Bitmap bmap = imageview.getDrawingCache(); 


          Intent i = new Intent(Lista_productos.this, pruebalista.class);        
          Bundle extras = new Bundle(); 
         // extras.putParcelable("imagebitmap", image); 
          i.putExtra("BitmapImage", bmap); 
          i.putExtras(extras); 

          i.putExtra("URL", URL); 


          startActivity(i); 


          }  
     }); 
    } 

    /** AsyncTask to download and load an image in ListView */ 
    private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{ 

     @Override 
     protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) { 

      InputStream iStream=null; 
      String imgUrl = (String) hm[0].get("flag_path"); 
      int position = (Integer) hm[0].get("position"); 

      URL url; 
      try { 
       url = new URL(imgUrl); 

       // Creating an http connection to communicate with url 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

       // Connecting to url     
       urlConnection.connect(); 

       // Reading data from url 
       iStream = urlConnection.getInputStream(); 

       // Getting Caching directory 
       File cacheDirectory = getBaseContext().getCacheDir(); 

       // Temporary file to store the downloaded image 
       File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");    

       // The FileOutputStream to the temporary file 
       FileOutputStream fOutStream = new FileOutputStream(tmpFile); 

       // Creating a bitmap from the downloaded inputstream 
       Bitmap b = BitmapFactory.decodeStream(iStream);    

       // Writing the bitmap to the temporary file as png file 
       b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);    

       // Flush the FileOutputStream 
       fOutStream.flush(); 

       //Close the FileOutputStream 
       fOutStream.close();    

       // Create a hashmap object to store image path and its position in the listview 
       HashMap<String, Object> hmBitmap = new HashMap<String, Object>(); 

       // Storing the path to the temporary image file 
       hmBitmap.put("flag",tmpFile.getPath()); 

       // Storing the position of the image in the listview 
       hmBitmap.put("position",position);    

       // Returning the HashMap object containing the image path and position 
       return hmBitmap;     


      }catch (Exception e) {    
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(HashMap<String, Object> result) { 
      // Getting the path to the downloaded image 
      String path = (String) result.get("flag");   

      // Getting the position of the downloaded image 
      int position = (Integer) result.get("position"); 

      // Getting adapter of the listview 
      SimpleAdapter adapter = (SimpleAdapter) mListView.getAdapter(); 

      // Getting the hashmap object at the specified position of the listview 
      HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position); 

      // Overwriting the existing path in the adapter 
      hm.put("flag",path); 

      // Noticing listview about the dataset changes 
      adapter.notifyDataSetChanged(); 
     } 

    } 

    } 

}

사람이이 문제를 해결하기 위해 어떤 생각을 가지고 있습니까? 감사!

+0

'Lista_productos'의 행 번호 ** 313 **은 무엇입니까? – Hariharan

답변

1

이 시도 ..

을 나는 당신은 AsyncTask를

내부
mListView.setOnItemClickListener(new OnItemClickListener() { 

OnItemClickListener를 만드는 방법이 필요없이 같은 DownloadTask

// Getting a reference to ListView of activity_main 
    mListView = (ListView) findViewById(R.id.listaJson); 

    Button Buttonback = (Button) findViewById(R.id.back); 

// Creating a new non-ui thread task to download json data 
    DownloadTask downloadTask = new DownloadTask(); 

    // Starting the download process 
    downloadTask.execute(strUrl); 

같은를 호출하기 전에 목록보기를 초기화해야 추측 당신 내부에서해야합니다. OnCreate