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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:43:24+00:00 2026-06-14T19:43:24+00:00

Im new to Android and I need a little help. I have a class

  • 0

Im new to Android and I need a little help. I have a class witch values from one table located on remote mysql and its working fine on 2.3 but on 4.x Im getting errors when I start it. I have read somewhere its because I`m not using AsyncTask. I tried to implement it on existing working code but there is one error, so please help because I tried everything to get this to work but no success.

Here is the existing code that works on 2.3

package com.stole.fetchtest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class FetchData extends ListActivity {

    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        String result = null;
        InputStream is = null;
        StringBuilder sb = null;
        ArrayList<?> nameValuePairs = new ArrayList<Object>();
        List<String> r = new ArrayList<String>();

        try {


            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://www.url.com/fetch.php");
            httppost.setEntity(new UrlEncodedFormEntity(
                    (List<? extends NameValuePair>) nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                    .show();
        }


        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"));

            sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();

            result = sb.toString();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                    .show();
        }

        try {
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data = null;
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                r.add(json_data.getString("names"));
            }
            setListAdapter(new ArrayAdapter<String>(this, R.layout.names_row, r));
        } catch (JSONException e1) {
            Toast.makeText(getBaseContext(), e1.toString(), Toast.LENGTH_LONG)
                    .show();
        } catch (ParseException e1) {
            Toast.makeText(getBaseContext(), e1.toString(), Toast.LENGTH_LONG)
                    .show();
        }

    }}

And then I tried to add AsyncTask, according to some tutorials found online, and it`s looking like this:

package com.stole.fetchtest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.ListActivity;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class FetchData extends ListActivity {

    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
    ArrayList<?> nameValuePairs = new ArrayList<Object>();
    List<String> r = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        new task().execute();
    }

    public class task extends AsyncTask<String, String, Void> {

        @SuppressWarnings("unchecked")
        @Override
        protected Void doInBackground(String... params) {

            try {

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.url.com/fetch.php");
                httppost.setEntity(new UrlEncodedFormEntity(
                        (List<? extends NameValuePair>) nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.toString(),
                        Toast.LENGTH_LONG).show();
            }

            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "iso-8859-1"));

                sb = new StringBuilder();

                String line = null;

                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }

                is.close();

                result = sb.toString();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.toString(),
                        Toast.LENGTH_LONG).show();
            }

            return null;
        }

        protected void onPostExecute(Void v) {

            try {
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data = null;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    r.add(json_data.getString("naziv"));
                }
                setListAdapter(new ArrayAdapter(this, R.layout.names_row, r));
            } catch (JSONException e1) {
                Toast.makeText(getBaseContext(), e1.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (ParseException e1) {
                Toast.makeText(getBaseContext(), e1.toString(),
                        Toast.LENGTH_LONG).show();
            }

        }

    }

}

The error I`m getting is at

setListAdapter(new ArrayAdapter(this, R.layout.names_row, r));

and it says “The constructor ArrayAdapter(FetchData.task, int, List) is undefined”
Thanks!

  • 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-14T19:43:25+00:00Added an answer on June 14, 2026 at 7:43 pm

    it should use the activity context not the task context..

    setListAdapter(new ArrayAdapter(FetchData.this, R.layout.names_row, r));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to android and need help on this. I have two views
I need little bit help related to android tabhost. I have 3 tabs and
I'm new to android and I'm need a little help understanding how to move
I need a little help with sending an HttpUrlConnection from my android application. Till
Hello i am new in android and i really need help with something. I
I'm new at android. I have a little idea over sharedPreference. Some tutorials say
I'm new to android and need help, as the question states i want to
I need a little help here.So basically I have to make a test of
I am new to android and only have a little experience with HTML and
I need a little help with some information/help about LocationManager in Android. I'm 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.