1

I want to update my database having 2 fields serial and ip using an Android app. But it's not updating. When I try to run the app, "Unfortunately App has stopped" pops up on my emulator

My PHP code

<?php
$serial=$_POST["serial"]; 
$ip=$_POST["ip"];

$con=mysqli_connect("mysql9.000webhost.com","~HIDDEN~","password","~HIDDEN~");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"INSERT INTO ip (serial, ip)
VALUES ('".$serial"', '".$ip"')");


mysqli_close($con);
?>

Main Activity (Android)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        postData();
    }    
    public void postData() {

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://ankit.host56.com/");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("serial", "23"));
            nameValuePairs.add(new BasicNameValuePair("ip", "24"));
            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
        }
    }
}

Layout file

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

</RelativeLayout>
  • 2
    move your `postData` to `AsynTask` and try to avoid anything related to Network Communication in UI thread. [Read More about `AsyncTask`](http://developer.android.com/reference/android/os/AsyncTask.html) – Sudhir Mishra Apr 06 '14 at 05:10
  • Now I app isn't stopping but the DB is not updating – user3293462 Apr 06 '14 at 05:43
  • You need to debug things, first see what response you get in the client. Next, check if the PHP script is error free. Try running your PHP code with static value for the dryRun. This will narrow down the problem. – Sudhir Mishra Apr 06 '14 at 05:47

0 Answers0