Attempting To Follow Basic How To Connect Android With Php, Mysql Tutorial: Application Crashes - Cannot Import Android.os.strictmode
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:
- Initialise your widgets in
onCreate()
method. - When you want to update them with data retrieved from
doInBackground()
method, callpublishProgress()
that invokeonProgressUpdate()
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"