
Conexión con base de datos mysql
Publicado por Gonzalo (4 intervenciones) el 09/09/2016 05:34:10
Buenas tengo un incoveniente para conectar desde mi aplicación a la base de datos mysql
Valora esta pregunta


0
formacion.example;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.concurrent.ExecutionException;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import android.widget.EditText;
public class Control extends AsyncTask<String, Void, String> {
private Context context;
public Control(Context context) {
this.context = context;
}
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
String pn = (String)arg0[0];
String pa = (String)arg0[1];
String ptutor = (String)arg0[2];
String pdni = (String)arg0[3];
String pdir = (String)arg0[4];
String ptel = (String)arg0[5];
String link;
String data;
BufferedReader bufferedReader;
String result;
try {
data = "?nombre" + URLEncoder.encode(pn, "UTF-8");
data += "&apellido=" + URLEncoder.encode(pa, "UTF-8");
data += "&tutor=" + URLEncoder.encode(ptutor, "UTF-8");
data += "&dni=" + URLEncoder.encode(pdni, "UTF-8");
data += "&dir=" + URLEncoder.encode(pdir, "UTF-8");
data += "&tel=" + URLEncoder.encode(ptel, "UTF-8");
link = "http://127.0.0.1/PutData.php?nombre="+data;
//+pn+"&apellido="+pa+"&tutor="+ptutor+"&dni="+pdni+"&dir="+pdir+"&tel="+ptel;
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
result = bufferedReader.readLine();
return result;
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
String jsonStr = result;
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String query_result = jsonObj.getString("query_result");
if (query_result.equals("SUCCESS")) {
Toast.makeText(context, "Data inserted successfully. Signup successfull.", Toast.LENGTH_SHORT).show();
} else if (query_result.equals("FAILURE")) {
Toast.makeText(context, "Data could not be inserted. Signup failed.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
}
}
}