Skip to content Skip to sidebar Skip to footer

Attempting To Follow Basic How To Connect Android With Php, Mysql Tutorial: Application Crashes - Cannot Import Android.os.strictmode

Ok - so I'm using the following tutorial: 'How to connect Android with PHP, MySQL' However almost everyone who attempts the tutorial (judging from the comments) is having force clo

Solution 1:

StrictMode.ThreadPolicypolicy=newStrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);

Since you are using AsyncTask there is no need to setting ThreadPolicy.And don't forget that StrictMode is available starting with 2.3 version.

Update:

Your app is not designated very well. Look at this

protectedStringdoInBackground(String... params) {

        runOnUiThread(newRunnable() { 
           ...
           txtName = (EditText) findViewById(R.id.inputName);
           txtPrice = (EditText) findViewById(R.id.inputPrice);
           txtDesc = (EditText)  findViewById(R.id.inputDesc);

           // display product data in EditText
           txtName.setText(product.getString(TAG_NAME));
           txtPrice.setText(product.getString(TAG_PRICE));
           txtDesc.setText(product.getString(TAG_DESCRIPTION));
           ...
        }
}

Why you're doing that? doInBackground() is directly designed for performing background operations also already runs on background Thread and you shouldn't perform UI update from it. You shouldn't mix it.

If you want to update UI, AsyncTask offers proper methods for achieve it:

  • onPreExecute()
  • onProgressUpdate()
  • onPostExecute()

Hence if you want to update your UI with some information about progress of background task call publishProgress(<data>) method that automatic invokes onProgressUpdate() method and from its update UI.

In your case you are performing initialisation of UI elements in doInBackground() method that you shouldn't.

I suggest you to make this:

  1. Initialise your widgets in onCreate() method.
  2. When you want to update them with data retrieved from doInBackground() method, call publishProgress() that invoke onProgressUpdate() method and here perform updating

Here is example:

@OverrideprotectedvoidonProgressUpdate(String... params) {
   txtName.setText(<value>);
   ...
}

Based on things mentioned above i guess that you need to read AsyncTask tutorial:

Solution 2:

Just make sure both your server and the android device is connected to the same wifi connection,make changes in the ip address as your server address for eg: change it to 192.168.1.102(whatever is shown on your server

The code works well and good i have tried it

Post a Comment for "Attempting To Follow Basic How To Connect Android With Php, Mysql Tutorial: Application Crashes - Cannot Import Android.os.strictmode"