Hola, mucho gusto
actualmente soy nuevo en programacion JAVA y estoy iniciando a programar en Android Con JAVA.
hoy tengo 1 problema, estaba haciendo una class para facilitarme las cosas a la hora de enviar peticiones POST. pero no me marca ningun error de Syntaxis, el problema viene cuando ejecuto la aplicacion y presiono el boton que ejecuta la Class
la Class JAVA es esta:
Código Java:
Ver originalpackage com.example.admin.noticias;
/**
* Created by Admin on 20/03/2015.
*/
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 org.apache.http.util.EntityUtils;
public class http_request {
List<NameValuePair> parametros;
protected void http_request(){
this.parametros = new ArrayList<NameValuePair>();
}
protected void url
(String url
) { this.url = url;
}
this.parametros.add(new BasicNameValuePair(key, value));
}
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(this.url);
httppost.setEntity(new UrlEncodedFormEntity(this.parametros));
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
String text
= EntityUtils.
toString(ent
); return text;
return "Error: "+e;
} catch (ClientProtocolException e) {
return "Error "+e;
return "Error: "+e;
}
}
}
donde la ejecuto es aqui:
Código Java:
Ver originalpublic class MainActivity extends ActionBarActivity {
private class enviar_post
extends AsyncTask
<Void,
String, String
> { @Override
http_request conexion = new http_request();
conexion.url("http://fxxxxxxx.com/android/login.php");
conexion.parametro("username", "Prueba 1");
conexion.parametro("password", "Prueba 2");
String result
= conexion.
post(); return result;
}
protected void onPostExecute
(String...
parms) { Intent ventana2 = new Intent(MainActivity.this, pantalla_2.class);
//Bundle contenido = new Bundle();
//contenido.putString("resultado", parms[0]);
//ventana2.putExtras(contenido);
startActivity(ventana2);
Toast.makeText(MainActivity.this, parms[0], Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton.
setOnClickListener(new View.
OnClickListener() { @Override
public void onClick
(View v
) { //Toast.makeText(MainActivity.this, "INICIANDO", Toast.LENGTH_SHORT).show();
new enviar_post().execute();
}
});
}
Donde dice "new enviar_post().execute();"
llamo al Metodo de DoBackground. todo sin errores de sintaxis pero la aplicaicon se Crashea al presionar el boton y en la consola me sale lo siguiente.
Process: com.example.admin.noticias, PID: 399
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(F utureTask.java:355)
at java.util.concurrent.FutureTask.setException(Futur eTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.jav a:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTas k.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.admin.noticias.http_request.parametro( http_request.java:34)
at com.example.admin.noticias.MainActivity$enviar_pos t.doInBackground(MainActivity.java:21)
at com.example.admin.noticias.MainActivity$enviar_pos t.doInBackground(MainActivity.java:16)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.jav a:237)
************at android.os.AsyncTask$SerialExecutor$1.run(AsyncTas k.java:231)
************at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1112)
************at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587)
************at java.lang.Thread.run(Thread.java:841)
Alguna idea?