– 我需要在fetchSchools中放入哪些参数
– 我需要在doInBackground中放入哪些参数?
我发现了许多“有用的”示例,但它们都在这些参数中使用了伪代码,并没有解释我实际需要放在那里的内容.
“我得到Eclipse错误的方法fetchSchools必须实现继承的抽象方法AsynchTask …”
我不需要传递任何东西,我希望它返回一个字符串.
public class fetchSchools extends AsyncTask<VoID,VoID,String> {public String doInBackground(String retval) { StringBuilder builder = new StringBuilder(); httpClIEnt clIEnt = new DefaulthttpClIEnt(); httpGet httpGet = new httpGet("http://www.domain/schools.PHP"); try { httpResponse response = clIEnt.execute(httpGet); Statusline statusline = response.getStatusline(); int statusCode = statusline.getStatusCode(); if (statusCode == 200) { httpentity entity = response.getEntity(); inputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new inputStreamReader(content)); String line; int a=0; while ((line = reader.readline()) != null) { builder.append(line); Log.i(MainActivity.class.getname(),"Reading in: " + a +" : "+ line); a++; } } else { Log.e(MainActivity.class.toString(),"Failed to download file"); } } catch (ClIEntProtocolException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } return builder.toString(); }protected voID onPostExecute() {}
}
解决方法 你给了doInBackground一个字符串参数,所以async task first参数必须是string not voID.AsyncTask<String,String> {
如果您不想传递参数,请不要为doInBackground函数提供参数.
检查此页面以获取asynctask参考:
http://developer.android.com/reference/android/os/AsyncTask.html
asynctask的第一个参数转到doInBackground函数,第二个参数转到onprogressUpdate函数,第三个参数转到onpostexecute函数.
我想你想这样做:
public class fetchSchools extends AsyncTask<VoID,String> { @OverrIDe protected String doInBackground(VoID... arg0) { StringBuilder builder = new StringBuilder(); httpClIEnt clIEnt = new DefaulthttpClIEnt(); // ... return builder.toString(); } protected voID onPostExecute(String retval) { } }总结
以上是内存溢出为你收集整理的android – AsyncTask – 扩展和doInBackground需要哪些参数?全部内容,希望文章能够帮你解决android – AsyncTask – 扩展和doInBackground需要哪些参数?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)