2013-08-17 74 views
2

我試圖從sdcard解析JSON。當我嘗試執行到的AsyncTask簡單的適配器,我收到以下錯誤:解析JSON時出錯

The method execute(String...) in the type AsyncTask<String,Void,SimpleAdapter> is not applicable for the arguments (StringBuilder) 

我花了一個多星期來解決這個代碼,但我couln't做到了。 我的代碼是:

public class Jeans extends Activity { 
    private final String JSON_file = "country.json"; 
    File jsonFile; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.baby1_1); 

     /** Getting Cache Directory */ 
     File cDir = ExternalStorage.getSDCacheDir(this, "json_files"); 

     /** Getting a reference to temporary file, if created earlier */ 
     jsonFile = new File(cDir.getPath() + "/" + JSON_file) ; 

     String strLine=""; 
     StringBuilder strJson = new StringBuilder(); 

     /** Reading contents of the temporary file, if already exists */ 
     try { 
      FileReader fReader = new FileReader(jsonFile); 
      BufferedReader bReader = new BufferedReader(fReader); 

      /** Reading the contents of the file , line by line */ 
      while((strLine=bReader.readLine()) != null ){ 
       strJson.append(strLine); 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 


    System.out.println(strJson); 


     /** The parsing of the xml data is done in a non-ui thread */ 
     // ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask(); 

     /** In here i'm getting the error */ 
     new ListViewLoaderTask().execute(strJson); 

    } 


    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]); 
       CountryJSONParser countryJsonParser = new CountryJSONParser(); 
       countryJsonParser.parse(jObject); 
      }catch(Exception e){ 
       Log.d("JSON Exception1",e.toString()); 
      } 

      CountryJSONParser countryJsonParser = new CountryJSONParser(); 

      List<HashMap<String, String>> countries = null; 

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

      /** Keys used in Hashmap */ 
      String[] from = { "country","flag","details"}; 

      /** Ids of views in listview_layout */ 
      int[] to = { R.id.tv_country,R.id.iv_flag,R.id.tv_country_details}; 

      /** Instantiating an adapter to store each items 
      * R.layout.listview_layout defines the layout of each item 
      */ 
      SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.lv_layout, from, to); 

      return adapter; 
     } 

     /** Invoked by the Android system on "doInBackground" is executed completely */ 
     /** This will be executed in ui thread */ 
     @Override 
     protected void onPostExecute(SimpleAdapter adapter) { 

      /** Getting a reference to listview of main.xml layout file */ 
      ListView listView = (ListView) findViewById(R.id.lv_countries); 

      /** Setting the adapter containing the country list to listview */ 
      listView.setAdapter(adapter); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

請人解決這個代碼我請。

回答

2

你所得到的錯誤,因爲你不是一個字符串傳遞。

做:

/** In here i'm getting the error */ 
    new ListViewLoaderTask().execute(strJson.toString()); 

請記住,當你使用對象,您需要使用toString()方法來創建一個字符串。

+0

非常感謝。這是我想要的。 – Kabil

+0

布萊恩請回答這個問題:http://stackoverflow.com/questions/18302964/need-help-on-json-parsing-2-issues – Kabil

6

錯誤的類型,應該是String

new ListViewLoaderTask().execute(strJson.toString()); 
+0

非常感謝。最後它的工作 – Kabil