Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to android development. Am making the app where in the user will get registered with us through the registration form in android. But the i don't get any errors nor i get the output.

Here's the source code for java:

Java
package com.example.entrepreneurexpress;

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

import org.apache.http.HttpEntity;
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 android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class InvestorRegister extends Activity {

	EditText YourName;
	EditText email;
	EditText password;
	EditText confirmPassword;
	Button btnClear, btnRegister;
	
	String nm, emailAdd, cnfPass, pwd;

	@SuppressLint({ "NewApi", "CutPasteId" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		setContentView(R.layout.investors_registration);
		ActionBar aBar = getActionBar();
		aBar.setDisplayHomeAsUpEnabled(true);

		YourName = (EditText) findViewById(R.id.invRegName);
		email = (EditText) findViewById(R.id.invRegEmail);
		password = (EditText) findViewById(R.id.invRegPassword);
		confirmPassword = (EditText) findViewById(R.id.invRegConfPassword);

		btnClear = (Button) findViewById(R.id.btnInvRegClear);
		btnClear.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (YourName.length() >= 1) {
					YourName.setText("");
				}
				if (email.length() >= 1) {
					email.setText("");
				}
				if (password.length() >= 1) {
					password.setText("");
				}
				if (confirmPassword.length() >= 1) {
					confirmPassword.setText("");
				}
			}
		});
		
		btnRegister = (Button) findViewById(R.id.btnInvRegRegister);
		btnRegister.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (  
						( YourName.getText().toString().equals("")) && 
						( password.getText().toString().equals("")) && 
						( confirmPassword.getText().toString().equals("")) && 
						( email.getText().toString().equals("")) 
					)
                {
                    Toast.makeText(getApplicationContext(),
                            "One or more fields are empty", Toast.LENGTH_SHORT).show();
                } else {
                	nm = YourName.getText().toString();
                	pwd = password.getText().toString();
                	cnfPass =  confirmPassword.getText().toString();
                	emailAdd = email.getText().toString();
                	
                	insertRecords(nm, pwd, emailAdd);
                }
			}
		});
	}
	
	private void insertRecords(String fullname, String passWord, String emailAddress) {
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
		nameValuePairs.add(new BasicNameValuePair("invName", fullname));
		nameValuePairs.add(new BasicNameValuePair("invEmail", emailAddress));
		nameValuePairs.add(new BasicNameValuePair("invPassword", passWord));		
		sendData(nameValuePairs);
	}
	
	private void sendData(ArrayList<NameValuePair> data) {
		try 
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/AdroidApp/insert.php");
            httppost.setEntity(new UrlEncodedFormEntity(data));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity httpEntity = response.getEntity();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


Here's the source code for php:
PHP
<?php

$output = array();

    $con = mysqli_connect("localhost", "root", "root", "AndroidApp");
    if(!$con) {
        echo "Connection Aborted !<br />" . mysqli_error($con);
    } else {
        echo "Connection Successful !<br />";
    }

    $name = isset($_POST['fullname']) ? $_POST['fullname'] : '';
    $email = isset($_POST['emailAddress']) ? $_POST['emailAddress'] : '';
    $pasword = isset($_POST['passWord']) ? $_POST['passWord'] : '';

    $query = mysqli_query($con, "INSERT INTO Investors
                                    (`invName`, `invEmail`, `invPassword`)
                                VALUES('".$name."', '".$email."', '".$pasword."')");

    if ($query)
    {
        $output["success"] = 1;
        $output["message"] = "Successfully Inserted";

        echo json_encode($output);
    } else {
        $output["success"] = 0;
        $output["message"] = "insertion failed......";

        echo json_encode($output);
    }

    mysqli_close($con);
?>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900