[Android Studio] jjmm1988:
hi

Im new in android studio,

I´m trying to send to a db but it not works and i dont know why. when i click the button it does nothing.

can somebody help?

thanks

my code:

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.pm.ActivityInfo;
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.Toast;

public class WebService extends Activity {
private EditText dni;
private EditText nombre;
private EditText telefono;
private EditText email;
private Button insertar;
private Button mostrar;
private ImageButton mas;
private ImageButton menos;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
setContentView(R.layout.fragment2);

dni=(EditText)findViewById(R.id.dni);
nombre=(EditText)findViewById(R.id.nombre);
telefono=(EditText)findViewById(R.id.telefono);
email=(EditText)findViewById(R.id.email);
insertar=(Button)findViewById(R.id.insertar);
insertar.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!dni.getText().toString().trim().equalsIgnoreCase("")||
!nombre.getText().toString().trim().equalsIgnoreCase("")||
!telefono.getText().toString().trim().equalsIgnoreCase("")||
!email.getText().toString().trim().equalsIgnoreCase(""))

new Insertar(WebServiceExample.this).execute();

else
Toast.makeText(WebServiceExample.this, "Hay informaci�n por rellenar", Toast.LENGTH_LONG).show();
}

});

}

private boolean insertar(){
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
HttpPost httppost;
httpclient=new DefaultHttpClient();
httppost= new HttpPost("myhost");
nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("dni",dni.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("nombre",nombre.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("telefono",telefono.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("email",email.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;
}
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();
nombre.setText("");
dni.setText("");
telefono.setText("");
email.setText("");
}
});
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;
}
}
}