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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:39:49+00:00 2026-06-17T19:39:49+00:00

I am trying to pass a string array to my adapter. My problem is

  • 0

I am trying to pass a string array to my adapter. My problem is i initialized globally and try to create string array in my asynchronous task below. But i am getting as null. Below is my code. Actually in this example they taking it from resource folders bu i want it from my json response. Any help is appreciated.

        String[] mString;
        public ActionsAdapter(Context context) {
             mInflater = LayoutInflater.from(context);
             session = new SessionManager(context);
             final Resources res = context.getResources();
             new ConnectAppMenu(context).execute();
             //  mTitles = res.getStringArray(R.array.actions_names);
            //  mUrls = res.getStringArray(R.array.actions_links);
            //  mIcons = res.obtainTypedArray(R.array.actions_icons);
             System.out.println("Menus"+ mString);

         }
         public class ConnectAppMenu extends AsyncTask<String, Void, String> {
    private ProgressDialog dialog;
    private final Context context;
    public ConnectAppMenu(Context context) {
        this.context = context;
    }
     @Override
        protected void onPreExecute() {
        // UI work allowed here
        dialog = new ProgressDialog(context);
        // setup your dialog here
        dialog.setMessage("Connecting....");
        dialog.setCancelable(false);
        dialog.show();
        }
    @Override
    protected String doInBackground(String... params) {
        String returnConnect = doConnectAppMenu();
        return returnConnect;
    }      
    public String doConnectAppMenu() {
        HashMap<String, String> user = session.getUserDetails();
        String client_url = user.get(SessionManager.KEY_CLIENT);
    //  if(connection) {
            HttpParams connectionParameters =  new BasicHttpParams(); 
            int timeoutConnection = 8000;
            HttpConnectionParams.setConnectionTimeout(connectionParameters, timeoutConnection);                 
            int timeoutSocket = 10000;
            HttpConnectionParams.setSoTimeout(connectionParameters, timeoutSocket);
            HttpClient httpClient   =   new DefaultHttpClient(connectionParameters);
            HttpPost httpPost       =   new HttpPost(client_url+"/api/common/app_menu");
            JSONObject json = new JSONObject();    
          try{
            json.put("data", 1);
             json.put("versionid", 1);                          
             StringEntity se = new StringEntity(json.toString());
             se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
             httpPost.setEntity(se);                       
             //Execute HTTP post request
             appmenu_res    =   httpClient.execute(httpPost); 
             appmenu_obj = new org.json.JSONObject(org.apache.http.util.EntityUtils.toString(appmenu_res.getEntity()));
             appmenu_result = appmenu_obj.toString();

           }
            catch(JSONException ex) {
                // TODO Auto-generated catch block
                ex.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
//  }           
        return appmenu_result;
    }
    @Override
    public void onPostExecute(String result) {

        int status_code = appmenu_res.getStatusLine().getStatusCode();                          
            if (status_code == 200) {
                dialog.dismiss();

                try {
                    menuObject = new JSONObject(result);
                    JSONArray names= menuObject.names();
                    JSONArray values = menuObject.toJSONArray(names);
                        for (int i = 0; i< values.length(); i++) {

                            JSONObject json2 = (JSONObject) values.get(i);
                            int menu_id = json2.getInt("menu_id");

                              if (menu_id > 0) {

                                  if (json2.has("menu_name")) {
                                     menu_list = json2.get("menu_name").toString();
                                     mString = new String[] { menu_list };

                                     //mUrls = menu_list.length();
                                  }

                              } 
                }
                        System.out.println("Json Menu" + Arrays.toString(mString));

                    /*Iterator<String> iter = menuObject.keys();
                    while (iter.hasNext()) {
                        String key = iter.next();
                        try {
                            Object value = menuObject.get(key);
                            //System.out.println("Hai" +value);
                            System.out.println("Post Execute" +  value);

                        } catch (JSONException e) {
                            // Something went wrong!
                        }
                    }*/
                } catch (JSONException e) { 
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //dialog.dismiss();

            }
    }

}
  • 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-17T19:39:50+00:00Added an answer on June 17, 2026 at 7:39 pm

    well first of all if you’re looking for the JSON object as a String don’t do what you did here:

    appmenu_obj = new org.json.JSONObject(org.apache.http.util.EntityUtils.toString(appmenu_res.getEntity()));
    

    I’d suggest doing the following:

    String Json = EntityUtils.toString(appmenu_res.getEntity());
    return Json;
    

    Now if you want to do the processing of your JSON on the UI thread (as you seem to want to based on the return type being a string) this should work. However this method is not recommended since the Json will need to be processed into objects which will take time and clog the UI thread.

    A better solution would be to serialize your Json on the background thread and then pass the serialized object back to the main thread to update the UI.

    If you have many types I would suggest using generics. I’ve already built a Loader which can do what you want if you want here. You will need touse the GSON library and build appropriate seralizers. Also working with the loader class is different to working with the AsyncTaskClass so please read the documentation here

    Edit

    Ok so what you want to do if you want get the Activity to have a callback from the AsyncTask is to do something along the lines of:

    public class MyActivity extends Activity implements AsyncTaskCallback
    

    where AsyncTaskCallback looks something like :

    public interface AsyncTaskCallback
    {
        public processData(Object responseObject);
    }
    

    now in your onPostExecute code you’ll need to do somehting like:

    @Override
    protected void onPostExecute(Object r){
        if (r != null) {
           l.processData(data);
        }
    }
    

    and add the following function to your async task

    public void addAsyncTaskListener (final AsyncTaskListener l){
        mCallback = l;
    }
    

    and then finally add the listner and process the data as required in the Activity in the function processData function that the interface forces your activity to implement.

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

Sidebar

Related Questions

I'm trying to pass a String array from one activity to another but when
1.I was trying to pass an array of string from 1 activity to another
I'm trying to pass a string to chdir(). But I always seem to have
I was trying to pass a string object to System.out.printf(...) in Java, but I
I am trying to pass a string literal to a Popen command, but it
I am trying to pass the value of a string from an array to
I'm trying to pass an array of string parameters to a C# ASP.NET web
I'm trying to pass through POST an array of checkboxes but this time it's
I am trying to pass a jquery string to my cakephp controller but I
I am trying to pass a string into my MySQLi prepared statement but it

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.