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

  • Home
  • SEARCH
  • 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 7248435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:08:16+00:00 2026-05-28T22:08:16+00:00

I am connecting to a server from an android device and querying the database

  • 0

I am connecting to a server from an android device and querying the database and displaying the result on the screen. However I want to display my result in a ListView. How can I get my code to display in a ListView? At the moment it just parses the data and displays it on the screen. How can I give each value of ContactName a reference value? Below is my code:

   public class DbConnectActivity extends Activity {
/** Called when the activity is first created. */

   TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Create a crude view - this should really be set via the layout resources 
    // but since its an example saves declaring them in the XML. 
    LinearLayout rootLayout = new LinearLayout(getApplicationContext()); 
    txt = new TextView(getApplicationContext()); 
    rootLayout.addView(txt); 
    setContentView(rootLayout); 

    // Set the text and call the connect function. 
    txt.setText("Connecting...");
  //call the method  to run the data retreival
    txt.setText(getServerData(KEY_121));



}
public static final String KEY_121 = "http://10.0.2.2/dbconnect.php"; //i use my real ip here



private String getServerData(String returnString) {

   InputStream is = null;
   String result = "";


    //the year data to send
   // ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    //nameValuePairs.add(new BasicNameValuePair("EngID","1"));

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(KEY_121);
            //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }

    //convert response to string
    try{

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
            //return result;


    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }
    //parse json data
    try{
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag","Name: "+json_data.getString("ContactName")
                           );
                    //Get an output to the screen
                    returnString += "\n\t" + jArray.getJSONObject(i);

            }
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return returnString;
}   

}
  • 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-28T22:08:17+00:00Added an answer on May 28, 2026 at 10:08 pm
    public class JsonExampleActivity extends ListActivity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.main);
            setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.fetchTwitterPublicTimeline()));
        }
    
        public ArrayList<String> fetchTwitterPublicTimeline()
        {
            ArrayList<String> listItems = new ArrayList<String>();
    
            try {
                URL twitter = new URL("your url here");
    
                URLConnection tc = twitter.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        tc.getInputStream()));
    
                String line;
                while ((line = in.readLine()) != null) {
                      JSONObject ja = new JSONObject(line);
                  /*
                    //for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = ja.getJSONObject("_api_error");
                        listItems.add(jo.getString("name"));*/
    
                        JSONArray jobj=ja.getJSONArray("artists");
                        //JSONArray ja = new JSONArray(line);
                    for (int i = 0; i < jobj.length(); i++) {
                        JSONObject jo = jobj.getJSONObject(i);
                        listItems.add(jo.getString("artist_name"));
                    }
                   // }
                }
            } catch (MalformedURLException 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 listItems;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to practice connecting from asp.net to a sql server database. i have
I want to access my android device (Nexus S) from my pc remotely. I
I'm connecting to a web server from my android application via HttpsUrlConnection or HttpUrlConnection
I am trying to upload file to a php server from my android device.
I'm building a client server application from an android device to a server over
I am connecting from Windows Server 2008 R2 to a Linux FTP Server running
stupid problem. I get those from a client connecting to a server. Sadly, the
I've implement simple udp server on my Android device.(sdk 1.5) it works fine when
I'm consuming a web service from an Android device using HTTP(s) with SSL. A
I am trying to send information to a server from an android phone. When

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.