我是一名新手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();
}
}
}
}
答案 0 :(得分:1)
哦,无论什么时候显示,都会发现堆栈跟踪错误不问题。我注意到在doInBackground
方法中捕获异常时忘记了打印错误,当我添加它时,我发现错误在于JSON请求的URL。嗯,承认这很令人尴尬,但无论如何我都解决了。