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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:47:47+00:00 2026-06-10T05:47:47+00:00

i am trying to submit user registration form in android with current location. I

  • 0

i am trying to submit user registration form in android with current location. I am new to android and java development. when i try to access myLat and myLan of the onLocationChanged method in my name value pairs code , it is unable to find both the variables. How can i access both the variables in my name value pair code.

package com.imran;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.apache.http.message.BasicNameValuePair;

import com.google.android.gcm.GCMRegistrar;
import com.google.android.maps.MyLocationOverlay;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class Register extends Activity {




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        final EditText email_id = (EditText)  findViewById(R.id.email_id) ;
        final EditText name = (EditText) findViewById(R.id.name);
        final EditText password = (EditText) findViewById(R.id.password);
        Button button = (Button) findViewById(R.id.button1) ;



        //generate GCM id
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, "12356");

        } else {
        String TAG = null;
        Log.v(TAG, regId);

        }
        //generate GCM id ended
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 
        //get current location

        LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener listner = new LocationListener() {

            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) {
                double myLonDouble = location.getLongitude();
            final String myLon = Double.toString(myLonDouble);
                double myLatDouble = location.getLatitude();
                final String myLat = Double.toString(myLatDouble);

            }
        };
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listner);



        //end get current location

        button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        //postData();
          HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://xyz.com/folder/register.php");

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("email", email_id.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("name", name.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("regid", regId));
                nameValuePairs.add(new BasicNameValuePair("uid", "2"));
                nameValuePairs.add(new BasicNameValuePair("lat",myLat ));
                nameValuePairs.add(new BasicNameValuePair("lon",myLon));
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
               //Toast.makeText(this, resId, duration)
                HttpResponse response = httpclient.execute(httppost);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }

    }
});

    }


}
  • 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-10T05:47:49+00:00Added an answer on June 10, 2026 at 5:47 am

    You should probably study up on scope and member variables. The thing is, you can’t declare one thing in one method then attempt to access it from another method.

    So, we declare that thing as a member variable, and define it in one method, then it can be used in another method.

    Like so (I’ve marked my comments with **):

    public class Register extends Activity {
    
        // "private" means it can only be accessed from this class
        private String myLat = null; // ** Declare myLat
        private String myLon = null; // ** Declare myLon
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            final EditText email_id = (EditText)  findViewById(R.id.email_id) ;
            final EditText name = (EditText) findViewById(R.id.name);
            final EditText password = (EditText) findViewById(R.id.password);
            Button button = (Button) findViewById(R.id.button1) ;
    
    
    
            //generate GCM id
            GCMRegistrar.checkDevice(this);
            GCMRegistrar.checkManifest(this);
            final String regId = GCMRegistrar.getRegistrationId(this);
            if (regId.equals("")) {
              GCMRegistrar.register(this, "12356");
    
            } else {
            String TAG = null;
            Log.v(TAG, regId);
    
            }
            //generate GCM id ended
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy); 
            //get current location
    
            LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            LocationListener listner = new LocationListener() {
    
                @Override
                public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onProviderEnabled(String arg0) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onProviderDisabled(String arg0) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onLocationChanged(Location location) {
                    double myLonDouble = location.getLongitude();
                    myLon = Double.toString(myLonDouble); // ** Define myLon
                    double myLatDouble = location.getLatitude();
                    myLat = Double.toString(myLatDouble); // ** Define myLat
    
                }
            };
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listner);
    
    
    
            //end get current location
    
            button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
            // TODO Auto-generated method stub
            // ** Check if they have been defined
            if (myLat == null || myLon == null)
                return;
    
            //postData();
              HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://xyz.com/folder/register.php");
    
                try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("email", email_id.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("name", name.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("regid", regId));
                nameValuePairs.add(new BasicNameValuePair("uid", "2"));
                nameValuePairs.add(new BasicNameValuePair("lat",myLat ));
                nameValuePairs.add(new BasicNameValuePair("lon",myLon));
                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
                // Execute HTTP Post Request
                   //Toast.makeText(this, resId, duration)
                HttpResponse response = httpclient.execute(httppost);
    
                } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                } catch (IOException e) {
                // TODO Auto-generated catch block
                }
    
            }
        });
    
        }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get my form to submit when a user either clicks
I am trying to submit a webform programmatically though android, where the user will
I have simple registration form. Once all information entered, user click submit button and
I am trying to create simple user registration form. I have an index.html file
I am trying to let a user submit a form that sends an email
Im trying to submit a specific form programatically, but I allways get the initial
I'm trying to submit a form (it's a dynamic form with fields added via
I'm trying to submit a form entry with an uploaded file, but I can't
I'm trying to submit a form when a select changes using jQuery Mobile. I
i'm trying to submit my Ajax form using jQuery. However calling the submit() function

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.