我试图与我的Android应用程序mysql建立连接。
我已经读过它是一个web服务。
我正在尝试将两个post变量发送到url中的php文件,以将此信息保存在mysql数据库中。
问题是它不发送任何东西。
我在android中开始aprogramar,为此我欣赏他的友好,并帮助学习和解决不完整的问题。
感谢您的所有意见和帮助。
这是我的类java:
package com.example.prueba;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class EnviarVariables extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main); // aqui decimos que esta clase manipula los objetos del activity_saludo.xml
Button btnSend = (Button) findViewById(R.id.button1);
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Insertar(EnviarVariables.this).execute();
}
});
}
private boolean insertar(){
EditText etNombre = (EditText) findViewById(R.id.editText1);
EditText etEmail = (EditText) findViewById(R.id.editText2);
HttpClient httpclient; //envia la info almacenada en httppost al webservice
List<NameValuePair> nameValuePairs;
HttpPost httppost; // almacena los datos a enviar, por medio de HttpClient
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://launidad.co/pruebaAndroid/index.php"); //url del servidor donde enviamos los datos
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("nombre",etNombre.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("email",etEmail.getText().toString().trim()));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
return true;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//AsyncTask para insertar Personas
class Insertar extends AsyncTask<String,String,String>{
private Activity context;
Insertar(Activity context){
this.context=context;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
if(insertar())
context.runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(context, "Persona insertada con éxito", Toast.LENGTH_LONG).show();
}
});
else
context.runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(context, "Persona no insertada con éxito", Toast.LENGTH_LONG).show();
}
});
return null;
}
}
}
答案 0 :(得分:0)
当然这不起作用。第一:您无法在UI线程中执行任何http请求。这是make things clearer。
的问题如果您想通过http发送/接收任何数据,请考虑以下sceanarios:
正如laalto建议的那样,请记录您的例外并找出它们的含义。