这是我第一次使用Android应用程序。我试图使用AsyncTask来填充listview。应用程序在尝试获取列表时崩溃。
这是我的代码,当我到达doINBackground()时,我尝试更新视图和应用程序崩溃。不确定我做错了什么。
class GetAllHomeStayPosts extends AsyncTask<All_Posts, Long, JSONArray>{
View view;
Activity a;
public GetAllHomeStayPosts(Activity context, View v){
this.view = v;
this.a = context;
}
@Override
protected JSONArray doInBackground(All_Posts... params) {
// TODO Auto-generated method stub
return params[0].GetAllPosts();
}
protected void onPostExecute(JSONArray jsonArray) {
setTextToTextView(jsonArray);
}
private void setTextToTextView(JSONArray jsonArray) {
// TODO Auto-generated method stub
String s = "";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = null;
try{
json = jsonArray.getJSONObject(i);
s = s + json.getString("Title") + "\n" +
"Address: " + json.getString("Address");
}catch(JSONException e){
e.printStackTrace();
}
}
}
访问数据库的All_posts类
public class All_Posts {
public JSONArray GetAllPosts(){
String url = "myurl.com/list.php";
HttpEntity httpEntity = null;
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray jsonArray = null;
if(httpEntity != null){
try{
String entityResponse = EntityUtils.toString(httpEntity);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonArray;
};
}
这就是我在片段
中执行AsyncTask的方式 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
//TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//Set up for slides
switch(getArguments().getInt(ARG_SECTION_NUMBER)){
case 1:{
rootView = inflater.inflate(R.layout.fragment_main, container, false);
//textView.setText("Home");
new GetAllHomeStayPosts(getActivity(), rootView).execute(new All_Posts());
break;
}
case 2: {
//textView.setText("Search");
rootView= inflater.inflate(R.layout.fragment_home_options, container, false);break;
}
}
return rootView;
}
这是logcat输出
06-10 21:53:08.422: E/AndroidRuntime(1086): Process: com.example.homestaymanager, PID: 1086
06-10 21:53:08.422: E/AndroidRuntime(1086): java.lang.RuntimeException: An error occured while executing doInBackground()
06-10 21:53:08.422: E/AndroidRuntime(1086): at android.os.AsyncTask$3.done(AsyncTask.java:300)
06-10 21:53:08.422: E/AndroidRuntime(1086): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-10 21:53:08.422: E/AndroidRuntime(1086): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-10 21:53:08.422: E/AndroidRuntime(1086): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-10 21:53:08.422: E/AndroidRuntime(1086): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-10 21:53:08.422: E/AndroidRuntime(1086): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-10 21:53:08.422: E/AndroidRuntime(1086): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-10 21:53:08.422: E/AndroidRuntime(1086): at java.lang.Thread.run(Thread.java:841)
06-10 21:53:08.422: E/AndroidRuntime(1086): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=162.252.240.170/list.php
06-10 21:53:08.422: E/AndroidRuntime(1086): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
06-10 21:53:08.422: E/AndroidRuntime(1086): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
06-10 21:53:08.422: E/AndroidRuntime(1086): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-10 21:53:08.422: E/AndroidRuntime(1086): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-10 21:53:08.422: E/AndroidRuntime(1086): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-10 21:53:08.422: E/AndroidRuntime(1086): at com.example.homestaymanager.All_Posts.GetAllPosts(All_Posts.java:26)
06-10 21:53:08.422: E/AndroidRuntime(1086): at com.example.homestaymanager.GetAllHomeStayPosts.doInBackground(MainActivity.java:282)
06-10 21:53:08.422: E/AndroidRuntime(1086): at com.example.homestaymanager.GetAllHomeStayPosts.doInBackground(MainActivity.java:1)
06-10 21:53:08.422: E/AndroidRuntime(1086): at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-10 21:53:08.422: E/AndroidRuntime(1086): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-10 21:53:08.422: E/AndroidRuntime(1086): ... 4 more
06-10 21:53:11.042: E/InputDispatcher(380): channel 'b2fd4858 com.example.homestaymanager/com.example.homestaymanager.LoginActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
06-10 21:53:11.052: E/InputDispatcher(380): channel 'b2fa04c8 com.example.homestaymanager/com.example.homestaymanager.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
06-10 21:53:11.052: E/InputDispatcher(380): channel 'b2fdd368 com.example.homestaymanager/com.example.homestaymanager.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
答案 0 :(得分:1)
您的网址字符串无效,请添加&#34; http://&#34;到你的URL字符串