Guranjan Singh
Guranjan Singh

Reputation: 734

Why do I get an error while executing doInBackGround?

This is my first time working on an android app. I'm trying to use AsyncTask to populate the listview. Application crashes when it tries to get the list.

Here is my code where I try to update the view and app crashes when it gets to doINBackground(). Not sure what I'm doing wrong.

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();
        }
    }
}

The All_posts class that accesses the database

   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;

};
  }

And this is how I'm executing the AsyncTask inside the fragment

   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;
    }

Here is the logcat output

    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!

Upvotes: 0

Views: 92

Answers (1)

ikhsan
ikhsan

Reputation: 810

your URL string is not valid, add "http://" to your URL string

Upvotes: 1

Related Questions