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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:29:33+00:00 2026-06-13T12:29:33+00:00

I debugged my Program and I noticed that I wasent able to run Networking

  • 0

I debugged my Program and I noticed that I wasent able to run Networking on the same Thread (I searched for this Error like 2 Days because in the Virtual Device the App worked without Problems -.- ). So now I know how I must fix it but I dont have a clue how I can give some parameteres that are not all String to the doinBackground Method.

My Method requires a url a method these i could access afaik with params[0] and params[1] in the doInBackground method. But whats with the List of NameValuePairs how can I access that in the doInBackground method?

Thank you very much for your help 🙂

This is my class:

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

public class JSONParser extends AsyncTask<String, Void, JSONObject>{

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // Funktion um JSON aus einer URL zu holen
    // Es wird ein POST oder ein GET Request gesendet
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // HTTP Request erstellen
        try {

            // Überprüfen welche Request Methode benutzt werden soll
            if(method == "POST"){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
                        CookiePolicy.BROWSER_COMPATIBILITY);
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }          

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Stream in ein String umwandeln
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Fehler!", "Fehler mein umwandeln von Stream in String: " + e.toString());
        }

        // JSON Object parsen
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // Das JSONObject zurückgeben
        return jObj;

    }

    @Override
    protected JSONObject doInBackground(String... params) {
        // TODO Auto-generated method stub
        return null;
    }
}
  • 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-13T12:29:34+00:00Added an answer on June 13, 2026 at 12:29 pm

    I don’t think you fully understand the full concept of AsyncTasks. You use these when you want to run an operation in a background thread and this is a very nice/flexible way of accomplishing this task. What is really nice to me is onPostExecute() executes on the main thread, so it can really do some powerful things once your work is completed in doInBackground(). You should keep in mind though that because onPostExecute() does execute on the main thread, you do not want to perform any networking operations here.

    Here is a simple example of an AsyncTask:

    private class myAsyncTask extends AsyncTask<String, Void, Boolean> {
    
        @Override
        protected void onPreExecute() {
            // before we start working
        }   
    
        @Override
        protected Boolean doInBackground(String... args) {
            //do work in the background
            return true;
        }
    
        @Override
        protected void onPostExecute(Boolean success) {
            // the work is done.. now what?
        }       
    }
    

    doInBackground() is where you are going to be doing the bulk of your work, so I will try to help you out with the basic structure you want. I just copied and pasted your code where I thought it should go so this is not 100% gauranteed, but hopefully it will help kick off what you want to do:

    private class JSONParser extends AsyncTask<String, Void, JSONObject> {
    
        static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";
    
        // variables passed in:
        String url;
        String method;
        List<NameValuePair> params;
    
        // constructor
        public JSONParser(String url, String method, 
            List<NameValuePair> params) {
            this.url = url;
            this.method = method;
            this.params = params;
        }
    
    
        @Override
        protected JSONObject doInBackground(String... args) {
            try {
                if(method == "POST"){
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
                            CookiePolicy.BROWSER_COMPATIBILITY);
                    HttpPost httpPost = new HttpPost(url);
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
    
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
    
                } else if(method == "GET"){
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    String paramString = URLEncodedUtils.format(params, "utf-8");
                    url += "?" + paramString;
                    HttpGet httpGet = new HttpGet(url);
    
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();
            } catch (Exception e) {
                Log.e("Fehler!", "Fehler mein umwandeln von Stream in String: " + e.toString());
            }
    
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
    
            return jObj;
        }
    
        @Override
        protected void onPostExecute(JSONObject obj) {
            // Now we have your JSONObject, play around with it.
        }       
    }
    

    Edit:

    I forgot to mention that you can also pass in args which is a string array. You can just create args and pass it in when you call your AsyncTask:

    new JSONParser(url, method, params).execute(args);
    

    and you can access args in doInBackground()

    Here is some more information on AyncTask: http://developer.android.com/reference/android/os/AsyncTask.html

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

Sidebar

Related Questions

I debugged that all and noticed, that validator constructor is called (and not one
I debugged the program and observed that it is stopped when it wants to
This is a behavior I noticed several times before and I would like to
I would like to run a GTK+/C program line by line with some debugger.
I've noticed that when I start up a program that sets up a couple
I've debugged my program and the arrays seem to be allocated well. However for
I'm getting an Error from Debugger: Error launching remote program: security policy error when
I have successfully debugged my own memory leak problems. However, I have noticed some
I'd like to detect when someone terminates a suspended debugged process without informing the
This is in Java, cross platform and being debugged on a computer running Ubuntu

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.