通过方法Post发送数据? (Android和PHP)

时间:2014-07-02 08:00:17

标签: java php android post http-post

问题是我无法通过POST方法发送数据

这些代码都没有帮助我通过post方法传递数据,运行时所有标记错误

代码:

public class Registro extends Activity{

protected String pagina = "http://192.168.0.5/vcard/index.php";
protected WebView web;
protected String qr = "70", firstname = "Name", lastname = "Last",
            email = "email@hotmail.es", institucion = "ITA", movil = "9136161";


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    web = (WebView) findViewById(R.id.webView1);

    postData();

    web.loadUrl(pagina);
}

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(pagina);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
                nameValuePairs.add(new BasicNameValuePair("qr", qr));
                nameValuePairs.add(new BasicNameValuePair("firstname", firstname));
                nameValuePairs.add(new BasicNameValuePair("lastname", lastname));
                nameValuePairs.add(new BasicNameValuePair("email", email));
                nameValuePairs.add(new BasicNameValuePair("institucion", institucion));
                nameValuePairs.add(new BasicNameValuePair("movil", movil ));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} }

其他代码

public class Registro extends Activity{

protected String pagina = "http://192.168.0.5/vcard/index.php";
protected WebView web;
protected String qr = "70", firstname = "Jonatan", lastname = "Flores",
            email = "quemeves@hotmail.es", institucion = "ITA", movil = "9136161";


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    web = (WebView) findViewById(R.id.webView1);

                     try
                     {
                        HttpURLConnection connection;
                           OutputStreamWriter request = null;

                                URL url = null;   
                                String response = null;         
                                String parameters = "qr="+qr+"&firstname="+firstname+"&lastname="+lastname+"&email="
                                +email+"&institucion="+institucion+"&movil="+movil;  

                          url = new URL(pagina);
                          connection = (HttpURLConnection) url.openConnection();
                          connection.setDoOutput(true);
                          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                          connection.setRequestMethod("POST");    

                          request = new OutputStreamWriter(connection.getOutputStream());
                          request.write(parameters);
                          request.flush();
                          request.close();            
                          String line = "";               
                          InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                          BufferedReader reader = new BufferedReader(isr);
                          StringBuilder sb = new StringBuilder();
                          while ((line = reader.readLine()) != null)
                          {
                              sb.append(line + "\n");
                          }
                          // Response from server after login process will be stored in response variable.                
                          response = sb.toString();
                          // You can perform UI operations here
                          Toast.makeText(this,"Message from Server: \n"+ response, 0).show();             
                          isr.close();
                          reader.close();

                      }
                      catch(IOException e)
                      {
                         {Toast.makeText(this,"Error 1", 0).show();  
                      }


                    }

    web.loadUrl(pagina);
}}

PHP数据

 <?php

require("LBHToolkit/vCard/Generator.php");



$nombres        = $_POST['qr']);
$firstname      = $_POST['firstname']);
$lastname       = $_POST['lastname']);
$email          = $_POST['email']);
$institucion    = $_POST['institucion']);
$movil          = $_POST['movil']);

... //code to generate a vCard

echo $vcard->create();

?>

最后,获取数据并下载vCard,但总是下载没有数据的vCard

1 个答案:

答案 0 :(得分:0)

对postData()使用AsyncTask类