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

The Archive Base Latest Questions

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

I have tried making a class supporting AsyncTask and I failed miserably. Later I

  • 0

I have tried making a class supporting AsyncTask and I failed miserably. Later I made a superclass of this class and added to my Main Class this:

//subclass of the superclass CustomHttpClient
CustomHttpClient2 cl= new CustomHttpClient2();
cl.execute();
response = cl.executeHttpPost("http://ironis.dyndns.org/aeglea/android/log.php", postParameters);

The error message is android.os.NetworkOnMainThreadException

The class I want to convert is this(I used it in sdk 7, but I moved to 14 and now it fails)

public class CustomHttpClient2 (extends CustomHttpClient)<<I used it without this {

 public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
    private static HttpClient mHttpClient;


    /**
     * Get our single instance of our HttpClient object.
     *
     * @return an HttpClient object with connection parameters set
     */
    private static HttpClient getHttpClient() {
        if (mHttpClient == null) {
            mHttpClient = new DefaultHttpClient();
            final HttpParams params = mHttpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
            ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
        }
        return mHttpClient;
    }

    /**
     * Performs an HTTP Post request to the specified url with the
     * specified parameters.
     *
     * @param url The web address to post the request to
     * @param postParameters The parameters to send via the request
     * @return The result of the request
     * @throws Exception
     */
    public JSONObject executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
        BufferedReader in = null;
        HttpEntity he=null;
        JSONObject jo=null;

        try {
            HttpClient client = getHttpClient();
            HttpPost request = new HttpPost(url);
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(formEntity);
            HttpResponse response = client.execute(request);
            //mine
            he=response.getEntity();
            jo=new JSONObject(EntityUtils.toString(he));
            //end mine

            return jo;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Performs an HTTP GET request to the specified url.
     *
     * @param url The web address to post the request to
     * @return The result of the request
     * @throws Exception
     */
    public static String executeHttpGet(String url) throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = getHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(url));
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();

            String result = sb.toString();
            return result;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

The other class I use CustomHttpClient is below

public class CustomHttpClient extends AsyncTask<HttpClient,String,String> {
public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
private static HttpClient mHttpClient;



protected HttpClient doInBackground(String... params) {
    if (mHttpClient == null) {
        mHttpClient = new DefaultHttpClient();
        final HttpParams params1 = mHttpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params1, HTTP_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params1, HTTP_TIMEOUT);
        ConnManagerParams.setTimeout(params1, HTTP_TIMEOUT);
    }
    return mHttpClient;
}



@Override
protected String doInBackground(HttpClient... params) {
    // TODO Auto-generated method stub
    return null;
}




}

Any love for me?

  • 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-10T08:28:06+00:00Added an answer on June 10, 2026 at 8:28 am

    Whoever has a similar problem I will post my solution.

    I modded the @sandrstar ‘s solution and I did that

    public class CustomHttpTask extends AsyncTask<Object,String,JSONObject> {
       public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
       private JSONObject response = null;
    
       @Override
       protected JSONObject doInBackground(Object... params) {
           //subclass of the superclass CustomHttpClient
           //CustomHttpClient cl= (CustomHttpClient) params[0];
           String url = (String) params[0];
           ArrayList<NameValuePair> postParameters = (ArrayList<NameValuePair>) params[1];
        try {
            response = CustomHttpClient.executeHttpPost(url, postParameters);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
            // say, you need response string back
    
            return response;
    }
    
        @Override
        protected void onPostExecute(JSONObject result) {
    
        }
        public JSONObject getval(){
            return response;
        }
    
    }
    

    Now I can do multiple posts in different url’s. The problem is that I want a json object. There are 2 ways. Either create a function as I did. or use AsyncTask’s get() function

    CustomHttpTask asdf = new CustomHttpTask();
    response = asdf.execute("http://192.168.1.4/aeglea/android/log.php", postParameters).get();
    

    That executes first and when it finishes it executes the get part. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried making a fiddle for this, but it's been too difficult to
I tried making a custom configuration class that went on like this: WatcherServiceInfoSection<TWatcherServiceDetailElement> where
I have tried making every div 75px and the table within the div but
I am making a GUI in OpenGL (more specifically lwjgl). I have tried hard
I'm having trouble making python print out texts properly aligned. I have tried everything
Have tried to find solutions for this and can't really come up with anything.
I have tried searching over the internet about this problem but not able to
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I have a class called GUIMain which registers, creates, and shows a main window
I have tried making (my first) a C# program: using System; using System.Collections.Generic; using

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.