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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:35:39+00:00 2026-06-05T05:35:39+00:00

Let me describe my application , I am fetching data from the website JSON

  • 0

Let me describe my application ,
I am fetching data from the website JSON url (Drupal website) , data is in JSON format.
In my application login functionality works perfectly . & user is validated on the server.
I am also fetching other data(JSON url) from the server & displaying in my android application.
Now, the problem is that I can’t access JSON data of pages , where login is required , because my login is not maintaining throughout the android application.

I have searched on stackoverflow & google I got these links & tried but don’t know how to use them in my code:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html

Android session management

Http cookie store in Android

Here is the Empty JSON that is from drupal site without login.

{
    "nodes": []
}

Here is the JSON from drupal site- after login(http://www.mywebsite.com/user/login) & reload the page http://www.mywebsite.com/myaccount-page on the site – in computer webbrowser . means computer web browser automatically maintains the login session.

{
"nodes": [
    {
        "node": {
            "Uid": "51",
            "Username": "anand",
            "Name": "anand",
            "Address": "\n\tAt- vadodara Nr. Kareli Baugh",
            "Date of Birth": "1998-08-20",
            "Occupation": "student",
            "Member Since": "36 weeks 6 days"
        }
    }
 ]
}

But in android application it does not do this automatically.
So I want to maintain this session in Android so that I can login in android application , after login redirect to another page-activity & get JSON data there.
Here is my code:

LoginActivity.java

     public void onClick(View v) {


            String uName = editUser.getText().toString();
            String Password = editPass.getText().toString();

            if(uName.equals("") | Password.equals(""))
            {
                Toast.makeText(getApplicationContext(), "Enter the Username and Password",Toast.LENGTH_SHORT).show();
            }
            else{


                                     String strResponse = util.makeWebCall(loginURL,uName,Password);
                 System.out.println("=========> Response from login page=> " + strResponse);


                try{
                    if (strResponse.substring(KEY_SUCCESS) != null) {
                        txterror.setText("");



                         Intent inlogin = new Intent(LoginActivity.this,
                                post_myprofile.class);
                        inlogin.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(inlogin);

                        //finish();
                    }
                    else
                    {
                        txterror.setText("Username and Password Not valid !!!");
                    }
                }
                catch (Exception e) {
                    // TODO: handle exception
                }





            }


        }
    });

    btngotoregister.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent1 = new Intent(getApplicationContext(),
                    RegisterActivity.class);
            // intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent1);

        }
    });
 }
 }

makeWebCall method in util.java

util.java

 public static String makeWebCall(String url, String uname,String pass)
{
    DefaultHttpClient client = new DefaultHttpClient();

    HttpPost post = new HttpPost(url);

     List<NameValuePair> params = new ArrayList<NameValuePair>();

     params.add(new BasicNameValuePair("username",uname));
        params.add(new BasicNameValuePair("password",pass));

    UrlEncodedFormEntity formEntity = null;
    try {
        formEntity = new UrlEncodedFormEntity(params);
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    post.setEntity(formEntity);

    try {
        //post.setEntity(new StringEntity(requestString));

        HttpResponse response = client.execute(post);
        System.out.println("=========> Responsehello => "+response);
        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode == HttpStatus.SC_OK)
        {
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            return iStream_to_String(is);
        }
        else
        {
            return "Hello This is status ==> :"+String.valueOf(statusCode);
        }
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

Now with this code login is successful & I got the JSON response from the server with detail.
& page-activity redirects to 2nd page for user profile.
On 2nd page I am not getting the userprofile JSON data-As mentioned above , I am getting Blank JSON because session is not maintained.

Here is the code of the 2nd page-activity.

post_myprofile.java

protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        String url = "http://www.cheerfoolz.com/myaccount-page";
        String strResponse = util.makeWebCall(url);

        try {
            JSONObject objResponse = new JSONObject(strResponse);

            JSONArray jsonnodes = objResponse
                        .getJSONArray(API.cheerfoolz_myprofile.NODES);

makewebcall method for profile in util.java

util.java

 public static String makeWebCall(String url) {

    DefaultHttpClient client = new DefaultHttpClient();

    HttpGet httpRequest = new HttpGet(url);
  //  HttpPost post = new HttpPost(url);

    try {

        HttpResponse httpResponse = client.execute(httpRequest);

         final int statusCode = httpResponse.getStatusLine().getStatusCode();

         if (statusCode != HttpStatus.SC_OK) {
          /*  Log.i(getClass().getSimpleName(),
                "Error => " + statusCode + " => for URL " + url);*/
            return null;
         }

         HttpEntity entity = httpResponse.getEntity();
            InputStream is = entity.getContent();
            return iStream_to_String(is);
    }
    catch (IOException e) {
        httpRequest.abort();
      // Log.w(getClass().getSimpleName(), "Error for URL =>" + url, e);
    }

    return null;

 }


public static String iStream_to_String(InputStream is1)
{
     BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096);
     String line;
     StringBuilder sb =  new StringBuilder();
     try {
        while ((line = rd.readLine()) != null) {
                sb.append(line);
         }
         rd.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     String contentOfMyInputStream = sb.toString();
     return contentOfMyInputStream;
}


}

I am getting blank JSON here in this page – that I have mentioned above. so how to maintain session in this user profile activity & get the data?

Thanks For listening.

  • 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-05T05:35:41+00:00Added an answer on June 5, 2026 at 5:35 am

    finally Its working for me 🙂

    instead of using new DefaultHttpClient all the time , I have made it static & used just once.

    static DefaultHttpClient client = new DefaultHttpClient();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me describe my question - I have a Java application - Hibernate as
I am facing a really strange problem with Core Data. Let's describe it: Definitions
Let me first describe the problem. I have this application that opens a word
Let's say you have a form with a component that displays data from a
Let me describe my situation: We are developing a web application that creates websites.
A big part of my C++ application uses classes to describe the data model,
Let me describe a simple use-case: Running all tests in our project may take
Thanks for viewing my question. Let me describe the app first. I have a
I have a problem with forwarding to a .jsp page. Let me describe my
Let me first describe my goal: I have created an object with 3 properties:

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.