Java 解析JSON时出错
我正在尝试从sdcard解析JSON。当我尝试执行AsyncTask Simple Adapter时,出现以下错误:Java 解析JSON时出错,java,android,json,android-layout,android-intent,Java,Android,Json,Android Layout,Android Intent,我正在尝试从sdcard解析JSON。当我尝试执行AsyncTask Simple Adapter时,出现以下错误: The method execute(String...) in the type AsyncTask<String,Void,SimpleAdapter> is not applicable for the arguments (StringBuilder) AsyncTask类型中的方法execute(String…)不适用于参数(StringBuilder)
The method execute(String...) in the type AsyncTask<String,Void,SimpleAdapter> is not applicable for the arguments (StringBuilder)
AsyncTask类型中的方法execute(String…)不适用于参数(StringBuilder)
我花了一个多星期的时间来解决这个问题,但我没能成功。
我的代码是:
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;
}
}
公共类活动{
私有最终字符串JSON_file=“country.JSON”;
文件jsonFile;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.baby1_1);
/**获取缓存目录*/
文件cDir=ExternalStorage.getSDCacheDir(这是“json_文件”);
/**获取对临时文件的引用(如果先前创建)*/
jsonFile=new文件(cDir.getPath()+“/”+JSON_文件);
字符串strLine=“”;
StringBuilder strJson=新的StringBuilder();
/**正在读取临时文件的内容(如果已存在)*/
试一试{
FileReader fReader=新的FileReader(jsonFile);
BufferedReader bReader=新的BufferedReader(fReader);
/**逐行读取文件的内容*/
而((strLine=bReader.readLine())!=null){
strJson.append(strLine);
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
系统输出打印LN(strJson);
/**xml数据的解析在非ui线程中完成*/
//ListViewLoaderTask ListViewLoaderTask=新建ListViewLoaderTask();
/**在这里,我得到了错误*/
新建ListViewLoaderTask().execute(strJson);
}
私有类ListViewLoaderTask扩展异步任务{
JSONObject jObject;
/**在非ui线程中解析xml数据*/
@凌驾
受保护的SimpleAdapter doInBackground(字符串…strJson){
试一试{
jObject=新的JSONObject(strJson[0]);
CountryJSONParser CountryJSONParser=新的CountryJSONParser();
parse(jObject);
}捕获(例外e){
d(“JSON例外1”,例如toString());
}
CountryJSONParser CountryJSONParser=新的CountryJSONParser();
列表国家=空;
试一试{
/**将解析后的数据作为列表构造获取*/
countries=countryJsonParser.parse(jObject);
}捕获(例外e){
Log.d(“异常”,例如toString());
}
/**Hashmap中使用的键*/
字符串[]from={“国家”、“国旗”、“详细信息”};
/**listview\u布局中的视图ID*/
int[]to={R.id.tv_country,R.id.iv_flag,R.id.tv_country_details};
/**实例化适配器以存储每个项
*R.layout.listview\u布局定义每个项目的布局
*/
SimpleAdapter=new SimpleAdapter(getBaseContext(),国家/地区,R.layout.lv_布局,从,到);
返回适配器;
}
/**由Android系统在“doInBackground”上调用,完全执行*/
/**这将在ui线程中执行*/
@凌驾
受保护的void onPostExecute(SimpleAdapter适配器){
/**获取对main.xml布局文件的listview的引用*/
ListView ListView=(ListView)findViewById(R.id.lv_国家);
/**将包含国家/地区列表的适配器设置为listview*/
setAdapter(适配器);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}
请有人帮我解这个代码。错误的类型,应该是
字符串
:
new ListViewLoaderTask().execute(strJson.toString());
由于未传入字符串,因此出现错误 做:
请记住,在处理对象时,需要使用
toString()
方法来创建字符串。非常感谢。这就是我想要的。布莱恩请回答这个问题:
/** In here i'm getting the error */
new ListViewLoaderTask().execute(strJson.toString());