0

i'm making an application in which i need to make a "register" activity which has few EditTexts. Now, the problem i'm facing is i need to send the data from EditText to some website. This website also has the same register page.

Please suggest some way how can i send the data to that particular column of website.

i have attached the pictures of the website enter image description here enter image description here img1 is the image of the website and img2 is the image of my application.

P.S. the website is in php and i don't have any relation with the person who owns this website. so i can't expect any help from him.

thanks in advance!

Saggy
  • 624
  • 8
  • 23
Akshay Sethi
  • 803
  • 3
  • 13
  • 22
  • have you tried get/post request in Java? its pretty simple. But first, your images don't show up. –  Jul 06 '13 at 17:07
  • sorry. now images edited.. and can you explain get/post request in detail..me new to android development. – Akshay Sethi Jul 06 '13 at 17:09
  • okay, so do you know the php script that actually does the registration? I mean do you know the url to that script? If you do, I can show you how to do a POST to that link in android. –  Jul 06 '13 at 17:13

1 Answers1

0

To read value from the EditText do this:

EditText edit   = (EditText)findViewById(R.id.edittext);
String name = edit.getText().toString();

Do this for all the other values in the form

Try using this function to post data to the script on the server that handles the registration.

public void postData(String name, String email, String password, String city, 
String phone, String, address, String pin) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/registration.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("Name", name));
        nameValuePairs.add(new BasicNameValuePair("Email", email));
        nameValuePairs.add(new BasicNameValuePair("Password", password));
        nameValuePairs.add(new BasicNameValuePair("City", city));
        nameValuePairs.add(new BasicNameValuePair("Phone", phone));
        nameValuePairs.add(new BasicNameValuePair("Address", address));
        nameValuePairs.add(new BasicNameValuePair("Pin", pin));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

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

You should run a check on the values that are being sent in this form before doing the actual posting, since I am assuming these values are going into a database and its good practice to check for valid data.

Also in your android manifest file, you should enable internet use.

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

Another point you should consider is that the network will not be available on the phone at all times. So you should check for the network state before making the calls above. You should set another permission :

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

Now you can use this function to poll for network availability:

public boolean checkNetwork() {
    ConnectivityManager connectivityManager = (ConnectivityManager) 
      getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }else{
        return false;
    }
}
  • and how do i link the button click in the app to the button click in the webpage? – Akshay Sethi Jul 07 '13 at 06:46
  • while implementing this i got android.os.NetworkOnMainThreadException please explain where i went wrong? – Akshay Sethi Jul 07 '13 at 06:47
  • Are you trying to do the networking calls on the main thread? You are not allowed to do that. Look at this question's answer for more details. http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception –  Jul 07 '13 at 08:58
  • Also to your earlier comment: You need to find out what webservice or php file the website is posting to for its registration module. You can use Firebug or inspect element in Chrome debugger and look for the form action url or look for an ajax post request in any of the javascripts on that page. Better yet you can ask the guy who wrote the website for the url. –  Jul 07 '13 at 09:01
  • So i will implement asynctask for this..thanks for this.. i have one more doubt..that is how can i link a button click from my app to the button click in website..i.e. the method u told will fill the entries on the website(if worked for me) but how to trigger to the submit button on the website because without it i wont be able to submit the details to the website.. – Akshay Sethi Jul 07 '13 at 09:05
  • Look at my last comment.. you have to find the link the website is posting its data to. –  Jul 07 '13 at 09:06
  • If i give u the link to the website can you help me with that? – Akshay Sethi Jul 07 '13 at 09:06
  • okay.. give me the link.. but you should do some research as well.. you cannot expect someone to take so much time out for your problems.. –  Jul 07 '13 at 09:08
  • i searched for the action url and i hope it is "code/reg.php". is this a correct action url? – Akshay Sethi Jul 07 '13 at 17:31
  • Probably it is the right one, but while calling it from within andriod you will have to put it along with the domain name: Example: http://yousite.com/code/reg.php. –  Jul 07 '13 at 17:46