I am trying to insert data to mysql database from my android app. I have php file code like this:
<?php
// Connection data
$host = 'xxxx';;
$uname = 'xxx';
$pwd = 'xxx';
$db = 'xxxx';
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$nombre=$_REQUEST['nombre'];
$asunto=$_REQUEST['asunto'];
$comentario=$_REQUEST['comentario'];
$flag['code']=0;
if($r=mysql_query("insert into comentarios (nombre, asunto, comentario) values('$nombre','$asunto','$comentario') ",$con))
{
$flag['code']=1;
echo"hi";
}
print(json_encode($flag));
mysql_close($con);
?>
And in my android app I am using this code:
class CreateNewComment extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage("Enviando comentario..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String nombre = inputnombre.getText().toString();
String asunto = inputasunto.getText().toString();
String comentario = inputcomentario.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("nombre", nombre));
params.add(new BasicNameValuePair("asunto", asunto));
params.add(new BasicNameValuePair("comentario", comentario));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), Comunidad.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
I always get the error "Invalid IP Address" and in the console I get the error that android.os.NetworkOnMainThreadException. Thanks.