0

I have an android app, i need to submit a user inputted name (example- paul,john) to a mysql database. To do this i have a simple php file that gets the name using a get method then inserts the name to the mysql table, this tested successfully in a my browser both on a computer and the android emulator browser. however its not working within the android app. App has a submit button, when clicked it should create the link,( example- http://10.0.2.2/project2/signup.php?name=john" ) and open a HttpURLConnection to add the name to the database using the php script. No matter what i do i cannot seem to get the inputted name to be inserted into my database.

 public void sign_up(View view)
{

    EditText newuserET = (EditText) findViewById(R.id.newuserET);
    String editTextValue = newuserET.getText().toString();

    //Toast.makeText(this, "Signing up...", Toast.LENGTH_SHORT).show();

    String link;
    String data;
    BufferedReader bufferedReader;
    String result;

    try {

       // data = "?name=" + URLEncoder.encode(fullName, "UTF-8");

        link = "http://10.0.2.2/project2/signup.php?name=" + editTextValue;

        //Toast.makeText(this, link, Toast.LENGTH_SHORT).show();

        URL url = new URL(link);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        con.setRequestMethod("GET");
        con.setDoOutput( true );

        // return result;
    } catch (Exception e)
    {
       // return new String("Exception: " + e.getMessage());
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }

}

I also added the following in the Android manifests- outside the application scope not inside

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Alvin
  • 1
  • 4

1 Answers1

0

First of All use the dependencies in android build.Gradle(Module.app)

dependencies{

compile 'com.loopj.android:android-async-http:1.4.9'

}

and Sync it.

Then go to the Script File.

public void Function()
{

     link = "http://10.0.2.2/project2/signup.php?name=" + editTextValue;
     AsyncHttpClient client = new AsyncHttpClient();

     client.get(link, new AsyncHttpResponseHandler() 
        {
           @Override
           public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
           {
                Toast.makeText(getApplicationContext(),"Uploaded",Toast.LENGTH_SHORT).show();
           }

           @Override
           public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
           {
               Toast.makeText(getApplicationContext(),"Error" + error.toString(),Toast.LENGTH_LONG).show();
           }
       });
}
Sayed Muhammad Idrees
  • 1,245
  • 15
  • 25