0

我是 Android 编程新手,但对 PHP 有相当的了解。

我正在尝试通过发布请求将数据发送到我的服务器。我有以下代码:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inps = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inps));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
    sb.append(line);
}
inps.close();
result=sb.toString();
Log.v("data-received",result);

我在互联网上的许多地方都找到了代码。

在 PHP 方面,我正在写这个:

<?php
    echo "something".$_REQUEST['year'];
?>

但是我只得到“某物”(在日志值“数据接收”中)作为我的应用程序端的输出?

我究竟做错了什么?我需要设置任何环境变量等吗?

4

4 回答 4

0

$_REQUEST并不总是支持、使用$_GET$_POST取决于您的参数所在的位置。

于 2013-05-17T15:01:25.697 回答
0

我使用这种和平的代码将日期发送到 php 文件。这是使用 POST。我希望你能用它做点什么...

URL url;
            try {
                url = new URL("url.nl/phppage.php");


              InputStream myInputStream =null;
                StringBuilder sb = new StringBuilder();
                        //adding some data to send along with the request to the server

    // Data below:  
    sb.append("email="+email.getText().toString()+"&name="+name.getText().toString()+"&message="+om.getText().toString());


                    //url = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setDoOutput(true);
                    conn.setRequestMethod("POST");
                    OutputStreamWriter wr = new OutputStreamWriter(conn
                            .getOutputStream());
                                // this is were we're adding post data to the request
                                wr.write(sb.toString());
                    wr.flush();
                    myInputStream = conn.getInputStream();
                    wr.close();
                    Context context = getApplicationContext();
                     Log.i("TAG", "Message send successful");
                CharSequence text = "Message send successful";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show(); 
                finish();
                } catch (Exception e) {
                      Context context = getApplicationContext();
                      Log.e("TAG", "Message not sended - SERVER POST ERROR");
                            CharSequence text = "Message couldn't be send.";
                            int duration = Toast.LENGTH_SHORT;

                            Toast toast = Toast.makeText(context, text, duration);
                            toast.show(); 


                }
于 2013-05-17T15:13:25.750 回答
0

在代码中已经是一个检查,他在成功时给出一个敬酒消息和一个日志。在php服务器上你可以使用print_r($_POST);这应该没问题。您现在使用的代码是什么?

于 2013-05-22T05:02:03.233 回答
0

我终于让它工作了。唯一的变化是在第一行:

List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePairs>();

代替

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

有人知道它为什么起作用吗?

于 2013-05-22T15:45:34.233 回答