0

I'm trying to make an android app which communicates with a php file/database.
I don't get it working, I'm completely new at this kind of android programming.
I already got this from a tutorial:

new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog,int id) {
 name = userInput.getText().toString();
 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://www.test.com/register.php");
 try {
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("name", name));
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);

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


}

PHP CODE:

// including database connection
$name= $_POST['name'];

$register= "INSERT INTO Users (Name, Level) VALUES(".$name.", 1)"; 
$result   = $conn->prepare($register);
$result->execute();

When I startup my app it instantly crashes.
What I'm doing wrong?
Does this code still work?
Did i forgot something?

4 Answers4

0

When I startup my app it instantly crashes.

You should call http requests in a background thread. You can use AsyncTask for that purpose.

 private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
      HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://www.test.com/register.php");
 try {
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("name", params[0]));
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);

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

        return "";
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}

You can run it using new LongOperation().execute(userinputString1);

Darish
  • 11,032
  • 5
  • 50
  • 70
0

All database communication should be done in the background. This can be done by using an AsyncTask or an other Worker. Also you need the Internet permission, add this line to the manifest :

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

But I'd recommend you to use a library like Retrofit for API calls. This spares you pain in the ass, and with the combination of GSON it is very powerfull.

jobbert
  • 3,297
  • 27
  • 43
0

First of all, Google AsynchTask in Android and learn what it is. And keep in mind all network related operations has to been done in a separate thread instead in the main android UI thread. So the code you are trying should be look like this

new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog,int id) {
 name = userInput.getText().toString();
 new CallPostService().execute();
}

class CallPostService extends AsyncTask<String, String, String> {
ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            progressDialog = new ProgressDialog(YourActivity.this);
            progressDialog.setMessage("Please wait...");
            progressDialog.show();
        }
        @Override
        protected String doInBackground(String... params) {
           HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://www.test.com/register.php");
 try {
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
      nameValuePairs.add(new BasicNameValuePair("name", name));
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);

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

        @Override
        protected void onPostExecute(String response) {

       progressDialog.dismiss();
    }

And remember to add the below line in manifest.xml file

<uses-permission android:name="android.permission.INTERNET"/>
Abid Khan
  • 2,451
  • 4
  • 22
  • 45
0

check this code, it will help you https://github.com/kosalgeek/generic_asynctask

Rihab
  • 57
  • 6