Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7612669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:58:20+00:00 2026-05-31T01:58:20+00:00

Ive made edits since the orignal post Im making an android app that connects

  • 0

Ive made edits since the orignal post Im making an android app that connects to an JSON API. Everything so far works ok except if there is a delay from the server. The UI of course can stop responding if it takes too long. Ive read that asynctask can solve my problem. Ive had a really ard time with the examples though.

Here is the restclient class making the http calls… parsing the json and storing custom objects to a public list that my other class can access.

package com.bde.dgcr;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class RestClient {
static List<ResponseHolder> list = new ArrayList<ResponseHolder>();
protected Context context = this.context;

private static String convertStreamToString(InputStream is) {
    /*
       * To convert the InputStream to String we use the BufferedReader.readLine()
       * method. We iterate until the BufferedReader return null which means
       * there's no more data to read. Each line will appended to a StringBuilder
       * and returned as String.
       */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}


/* This is a test function which will connects to a given
  * rest service and prints it's response to Android Log with
  * labels "Praeda".
  */
public void  connect(String url) {


    AsyncTask<String, Void, Void> connection = new AsyncTask<String, Void, Void>() {
        protected Context context;
        @Override
        protected Void doInBackground(String... params) {
// Prepare a request object
            HttpGet httpget = new HttpGet(params[0]);

// Execute the request
            HttpResponse response;
            HttpClient httpclient = new DefaultHttpClient();
            try {
                list.clear();
                response = httpclient.execute(httpget);
                // Examine the response status
                Log.i("Praeda", response.getStatusLine().toString());

                // Get hold of the response entity
                HttpEntity entity = response.getEntity();
                // If the response does not enclose an entity, there is no need
                // to worry about connection release

                if (entity != null) {

                    // A Simple JSON Response Read
                    InputStream instream = entity.getContent();
                    String result = convertStreamToString(instream);

                    // A Simple JSONObject Creation
                    //JSONObject json= new JSONObject(result);
                    JSONArray jsonArray = new JSONArray(result);


                    // A Simple JSONObject Parsing
                    for (int i = 0; i < (jsonArray.length()); i++) {
                        JSONObject json_obj = jsonArray.getJSONObject(i);
                        ResponseHolder rh = new ResponseHolder(json_obj);
                        list.add(rh);
                    }


                    instream.close();

                }

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
             ListView listView1;


            super.onPostExecute(aVoid);    //To change body of overridden methods use       File | Settings | File Templates.
            ResponseHolderAdapter adapter = new ResponseHolderAdapter(context, R.layout.listview_item_row, RestClient.list);
            listView1 = (ListView) findViewById(R.id.listView1);

            View header = (View)         getLayoutInflater().inflate(R.layout.listview_header_row, null);
            listView1.addHeaderView(header);

            listView1.setAdapter(adapter);
        }
    };
    connection.execute(url);

}
}

Here is the class calling the static connect method and using the list for an adapter to go in a list view.

public class JsonGrabber extends Activity {


private final String API_KEY = "key";
private final String SECRET = "secret";
private String state;
private String city;
private String country;
private static String mode;
private String md5;
private String url;
CourseSearch cs;
private ListView listView1;

/**
 * Called when the activity is first created.
 */


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.results);
    Bundle extras = getIntent().getExtras();
    state = extras.getString("state");
    city = URLEncoder.encode(extras.getString("city"));
    country = extras.getString("country");
    mode = extras.getString("mode");
    md5 = MD5.getMD5(API_KEY + SECRET + mode);
    System.out.println(md5);
    url = "http://www.api.com/?key=" + API_KEY + "&mode=" + mode + "&id=1&sig=" + md5;
    String findByLocUrl = "http://www.api.com/?key=" + API_KEY + "&mode=" + mode + "&city=" + city + "&state=" + state + "&country=" + country + "&sig=" + md5;
    System.out.println(findByLocUrl);
    RestClient rc = new RestClient();
    rc.connect(findByLocUrl);
    //RestClient.connect(findByLocUrl);
/*        if (RestClient.list.isEmpty())
    {
        setContentView(R.layout.noresults);
    }     else
    {

    ResponseHolderAdapter adapter = new ResponseHolderAdapter(this, R.layout.listview_item_row, RestClient.list);
    listView1 = (ListView) findViewById(R.id.listView1);

    View header = (View) getLayoutInflater().inflate(R.layout.listview_header_row, null);
    listView1.addHeaderView(header);

    listView1.setAdapter(adapter);
    }

 */
}


}

Somehow Im supposed to mesh all this up into using an innerclass extending asyctask to do the API calls in the background parse the json add to my list and set up the adapter. I know i may have some object orientation problems and was hoping before i continue on with my app that you guys can make sure im going in the right direction. I have a few other classes I didnt include. Let me know if it will make more sense if I added the other classes. Thanks In Advance for any help you guys/girls may be able to offer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:58:22+00:00Added an answer on May 31, 2026 at 1:58 am

    You can rewrite your connect method using an AsyncTask like this:

    public static void connect(String url) {
    
    HttpClient httpclient = new DefaultHttpClient();
    
    AsyncTask<String, Void, Void> connection = new AsyncTask<String, Void, Void>() {
    
    @Override
    protected Void doInBackground(String... params) {
    // Prepare a request object
    HttpGet httpget = new HttpGet(params[0]);
    
    // Execute the request
    HttpResponse response;
    try {
        list.clear();
    
        response = httpclient.execute(httpget);
        // Examine the response status
        Log.i("Praeda", response.getStatusLine().toString());
    
        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release
    
        if (entity != null) {
    
            // A Simple JSON Response Read
            InputStream instream = entity.getContent();
            String result= convertStreamToString(instream);
    
        // A Simple JSONObject Creation
        //JSONObject json= new JSONObject(result);
        JSONArray jsonArray = new JSONArray(result);
    
    
        // A Simple JSONObject Parsing
        for (int i = 0; i < (jsonArray.length()); i++) {
            JSONObject json_obj = jsonArray.getJSONObject(i);
            ResponseHolder rh = new ResponseHolder(json_obj);
            list.add(rh);
        }
    
    
         instream.close();
        }
        }
        } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    connection.execute(url);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to skin a vb.net app I made ive googled some stuff and
I made an image to easier explain what Im after: Image Illustration http://bayimg.com/image/eabahaaci.jpg Ive
I've made some unit tests (in test class). The tutorial I've read said that
I've made a custom DataGridViewCell that displays a custom control instead of the cell;
I've made an application that takes tagged versions of a project from an hg
It's been ages since I've written a COM dll. I've made a couple of
I'm working on a Python library that interfaces with a web service API. Like
I have written a web application that interacts with Google calendar (adds/edits/updates events). You
I've made some facebook pages before, but I'm running in to a problem that
I am currently designing an iOS 5 iPhone app that will use a .NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.