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 8413823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:00:05+00:00 2026-06-10T01:00:05+00:00

I’m trying to connect to a database on my server. I’ve only just started

  • 0

I’m trying to connect to a database on my server. I’ve only just started working with AsyncTask since it was recommend to me. Before that I had the HTTP request on the main UI thread which of course meant it didn’t work. It’s working a little better now but I’m still having problems. If my server is off I get a force close. If you look at my code below I included a toast to tell me if the connection could not be established. However this is not showing.

Is there something I need to change in order to get it to stop Force closing and just show the toast?

When I have my server on and the database is up I looked at the logcat and my android app does retrieve the data but it’s in the logcat and not presented on the page. Some of this code is from tutorials and searching google so I know that the “// PARSING DATA” bit only counts the length of the data at the moment and nothing else. I presume that’s where I need to add the code for it to be displayed. Just goes to show how new this is all to me.

Really getting frustrated by all this which is definitely not helping :(.

Here is my code:

package com.android.history;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class CurrentSeasonDrivers extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.currentseason_drivers);

    new HttpTask().execute();

}

private static class HttpTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Void doInBackground(Void... params) {
        doStuff();

        return null;
    }

    public static void doStuff() {
        JSONArray jArray;
        String result = null;
        InputStream is = null;
        StringBuilder sb = null;
        HttpResponse response;
        HttpEntity entity;

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        // HTTP POST REQUEST
        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost("http://192.168.0.13/testdatabase.php");

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response = httpclient.execute(httppost);

            entity = response.getEntity();

            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection" + e.toString());
            Toast.makeText(null, "Could not connect to server", Toast.LENGTH_LONG).show();
        }

        // CONVERT RESPONSE TO STRING
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            is.close();
            result = sb.toString();

            Log.i("json string", result);
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }


        // PARSING DATA
        String drivername;
        String drivesfor;
        try {
            jArray = new JSONArray(result);
            JSONObject json_data = null;

            System.out.println("Length " + jArray.length());
            Log.d("DB", "Length " + jArray.length());

            for (int i = 0; i < jArray.length(); i++) {

                System.out.println("counter " + i);
                json_data = jArray.getJSONObject(i);
                drivername = json_data.getString("Driver_full_name");
                drivesfor = json_data.getString("Drives_for");

                System.out.println("Drives_for" + drivesfor);
            }
        } catch (JSONException e1) {
            Log.d("DB", "Error somewhere");
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
    }
}
}

Here are the errors in my log_cat:

08-22 12:26:39.405: E/log_tag(14327): Error in http connectionorg.apache.http.conn.HttpHostConnectException: Connection to http://192.168.0.13 refused
08-22 12:26:39.405: W/dalvikvm(14327): threadid=13: thread exiting with uncaught exception (group=0x40a051f8)
08-22 12:26:39.455: E/AndroidRuntime(14327): FATAL EXCEPTION: AsyncTask #2
08-22 12:26:39.455: E/AndroidRuntime(14327): java.lang.RuntimeException: An error occured while executing doInBackground()
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.lang.Thread.run(Thread.java:856)
08-22 12:26:39.455: E/AndroidRuntime(14327): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.os.Handler.<init>(Handler.java:121)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.widget.Toast$TN.<init>(Toast.java:317)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.widget.Toast.<init>(Toast.java:91)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.widget.Toast.makeText(Toast.java:233)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at com.android.history.CurrentSeasonDrivers$HttpTask.doStuff(CurrentSeasonDrivers.java:74)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at com.android.history.CurrentSeasonDrivers$HttpTask.doInBackground(CurrentSeasonDrivers.java:44)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at com.android.history.CurrentSeasonDrivers$HttpTask.doInBackground(CurrentSeasonDrivers.java:1)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-22 12:26:39.455: E/AndroidRuntime(14327):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-22 12:26:39.455: E/AndroidRuntime(14327):    ... 5 more
  • 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-06-10T01:00:07+00:00Added an answer on June 10, 2026 at 1:00 am

    doInBackground runs on a separate thread. You cannot show a Toast from a chaild thread.

    use the following code instead, inside the catch block.

     runOnUiThread(new Runnable() {          
       public void run() {                 
        Toast.makeText(null, "Could not connect to server", Toast.LENGTH_LONG).show();     
     }            
    }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.