0

i want to store data from android app to my web server.mysql server not connected plz guide me where i upload my php code in cpanel. iam a newbie so plz help me experts. thanks in advance

    my xml layout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical" >

    <EditText
        android:id="@+id/uname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="First Name" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/sname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Last Name" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Email" />

    <EditText
        android:id="@+id/country"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Country" />

    <EditText
        android:id="@+id/role"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Role" />

    <Button
        android:id="@+id/savebtn"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Save" />

</LinearLayout>

//here my php code

<?
if( $_POST )
{
  $con = mysql_connect(""http://******","*******","******");

  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("****", $con);

  $first_name = $_POST['uName'];
  $last_name = $_POST['name'];
  $users_email = $_POST['email'];
  $users_region = $_POST['region'];
  $users_role = $_POST['role'];

  $first_name = mysql_real_escape_string($first_name);
  $last_name = mysql_real_escape_string($last_name);
  $users_email = mysql_real_escape_string($users_email);
  $users_region = mysql_real_escape_string($users_region);
  $users_role = mysql_real_escape_string($users_role);

  $articleid = $_GET['id'];
  if( ! is_numeric($articleid) )
    die('invalid article id');

  }else{ 
 $sql = "INSERT INTO contact (uname,sname,region,email,role) VALUES('$name','$username','$password','$email','role')";
 if(mysqli_query($con,$sql)){
 echo 'Data saved';
 }else{
 echo 'oops! Please try again!';
 }
 }
 mysqli_close($con);
 }
}else{
echo 'error';
}

// here is my java code

package com.english.golearncontact;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
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 MainActivity extends Activity implements OnClickListener  {
Button save;
EditText fname,sname,email,region,role;

private static final String REGISTER_URL ="http://golearn-english.com/docs/demo.php";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fname=(EditText) findViewById(R.id.uname);
        sname=(EditText) findViewById(R.id.sname);
        email=(EditText) findViewById(R.id.email);
        role=(EditText) findViewById(R.id.role);
        region=(EditText) findViewById(R.id.country);

        save=(Button) findViewById(R.id.savebtn);
        save.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            if(v == save){
                registerUser();
            }
        }

        private void registerUser() {

            String firstname = fname.getText().toString().trim().toLowerCase();
            String lastname = sname.getText().toString().trim().toLowerCase();
            String country = region.getText().toString().trim().toLowerCase();
            String mail = email.getText().toString().trim().toLowerCase();
            String rol= role.getText().toString().trim().toLowerCase();
            register(firstname,lastname,country,mail,rol);
        }


        private void register(String firstname, String lastname, String country, String mail, String rol) {
             String urlSuffix = "?firstname="+fname+"&lastname="+sname+"&country="+region+"&email="+email+"&role="+role;

            class RegisterUserClass extends AsyncTask<String, Void, String>{


                ProgressDialog loading;
                RegisterUserClass ruc = new RegisterUserClass();


                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    loading = ProgressDialog.show(MainActivity.this, "Please Wait",null, true, true);
                }

                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
                    loading.dismiss();
                    Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
                }

                @Override
                protected String doInBackground(String... params) {
                    String s = params[0];
                    BufferedReader bufferedReader = null;
                    try {
                        URL url = new URL(REGISTER_URL+s);
                        HttpURLConnection con = (HttpURLConnection) url.openConnection();
                        bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                        String result;

                        result = bufferedReader.readLine();

                        return result;
                    }catch(Exception e){
                        return null;
                    }
                }
            }

    }
        }







  [1]: https://i.stack.imgur.com/JLn1E.png
  • You are passing variables with URL (GET method) and expecting to receive it in PHP with POST method,, which won't work. – Vivek Av Jan 29 '16 at 15:59

1 Answers1

1

Your php waits for POST data, but your android code does not fill that hash data. For doing that look at the solution (has direct answer to your question):

android http post asynctask

I personally strongly advice using volley library instead of http request. Here is a good tutorial:

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

Also consider looking at:

Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

Google Volley vs Android-Async-Http

Those solutions have everything you need and more.

Community
  • 1
  • 1
erol yeniaras
  • 3,701
  • 2
  • 22
  • 40