miércoles, 26 de septiembre de 2012

Android: postData

Habilitar
android.permission.INTERNET


public void postData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        //HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
        HttpPost httppost = new HttpPost("http://192.168.0.18/test/androidpost.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            
            Toast.makeText(getBaseContext(), "Enviado con exito: "+response.toString(), Toast.LENGTH_LONG).show();
            
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            
            Toast.makeText(getBaseContext(), "Error 1: "+e.toString(), Toast.LENGTH_LONG).show();
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Toast.makeText(getBaseContext(), "Error 2: "+e.toString(), Toast.LENGTH_LONG).show();
        }
    }

No hay comentarios:

Publicar un comentario