我是一名新手 Android 开发人员,我正在尝试使用 GSON 和 AsynTask 从 RESTful Web 服务获取数据。但是,当调用包含 AsynTask 类的活动时,我在 LogCat 中得到的唯一错误是:
01-02 11:17:58.275: E/dalvikvm(807): 无法打开堆栈跟踪文件'/data/anr/traces.txt': 是一个目录
然后doInBackground
无法调用该方法,因此无法从 Web 服务获得响应。我已经用谷歌搜索了这个问题,但大多数人都得到了permission denied
或其他东西,不像is a directory
我的情况。因此,如果您对如何解决此问题有任何建议,请帮助我!
如果有人可能会要求提供包含 AsynTask 的类的代码,我将其包含在此处:
public class PlaceListActivity extends Activity{
private ListView m_vwPlaceLayout;
private ArrayList<Place> m_arrPlaceList;
private PlaceAdapter m_placeListAdapter;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.place_list);
m_vwPlaceLayout = (ListView) findViewById(R.id.spotListView);
m_arrPlaceList = new ArrayList<Place>();
m_placeListAdapter = new PlaceAdapter(this, R.layout.place_view, m_arrPlaceList);
m_vwPlaceLayout.setAdapter(m_placeListAdapter);
populateList();
}
public void populateList(){
Intent intent = getIntent();
String key = intent.getStringExtra("KEYWORD");
new GetSpots().execute(key);
}
private class GetSpots extends AsyncTask<String, Integer, ArrayList<Place>>{
protected ArrayList<Place> doInBackground(String... keys){
ArrayList<Place> list = null;
String url = "http://10.0.2.2:81/HGourmet/getPlaces?key=" + keys[0].trim();
try{
RestClient client = new RestClient(url);
try{
client.Execute(RequestMethod.GET);
}catch(Exception e){
e.printStackTrace();
}
String json = client.getResponse();
Gson gson = new Gson();
Type listType = new TypeToken<ArrayList<Place>>(){}.getType();
list = gson.fromJson(json, listType);
}catch(Exception e){}
return list;
}
protected void onProgressUpdate(Integer... progress){
}
protected void onPreExecute(){
}
protected void onPostExecute(ArrayList<Place> placeList){
if (placeList != null){
for (Place place : placeList){
m_arrPlaceList.add(place);
}
m_placeListAdapter.notifyDataSetChanged();
}
else{
Toast.makeText(PlaceListActivity.this, "No Results!", Toast.LENGTH_LONG).show();
}
}
}
}