【问题标题】:Posting using httppost not being received at server end在服务器端未收到使用 httppost 发布
【发布时间】:2013-05-17 14:59:40
【问题描述】:

我是 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'];
?>

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

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

【问题讨论】:

  • 内容类型设置为什么?需要特定于 PHP 才能确认。
  • 我也添加了内容类型..仍然无法正常工作...
  • 确保您的内容类型设置为“application/x-www-form-urlencoded”并且您的内容长度也已设置。我不太了解 Java API,无法说明它是否为您设置。在您的服务器端脚本中,执行以下操作:“print_r( $_SERVER ); print_r( $_POST );”。这将转储标题并将数据发布到屏幕,以便您可以直观地检查结果。让我知道你发现了什么。
  • 嘿伙计,你是为 DH4 做宝石的 Shubham 吗? ;)

标签: php android http-post httpclient


【解决方案1】:

$_REQUEST 并不总是受支持,请根据参数所在的位置使用$_GET 或$_POST。

【讨论】:

  • 您可以记录您发送的请求吗?也许你在构造请求时犯了一个错误。
  • 我也试过 print_r(var_dump($_REQUEST)), print_r(var_dump($_POST)) 和 print_r(var_dump($_GET))....它返回数组(0){}
  • 我发送的数据是前 2 行...我还需要发送其他任何内容吗?
  • 我的意思是,如果可以的话,显示 raw HTTP 请求。如果 HTTP 请求良好,则问题出在接收方。如果 HTTP 请求错误,则说明发送者有问题。
  • httppost.getRequestLine()?我猜这里另外,也许您应该使用setParams() 而不是setEntity? developer.android.com/reference/org/apache/http/…
【解决方案2】:

我使用这种和平的代码将日期发送到 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(); 


                }

【讨论】:

  • 什么是getApplicationContext()?
  • @Shubham 这只是一个获取应用程序上下文的函数。有时会在函数中给出,但如果不是,这是一种解决方法...
  • 我试过这段代码...它仍然无法正常工作...服务器有问题吗?
  • 您是否在模拟器中尝试过?因为我记得我也遇到过这个问题。
【解决方案3】:

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

【讨论】:

  • 我试过了...给了我一个空数组...请检查解决方案...您能解释一下为什么会这样吗?
  • 您现在使用的代码是什么?因为发布的代码对我有用。是不是你发送的数据有问题?
【解决方案4】:

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

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

而不是

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-06
    • 2015-10-24
    • 2013-11-25
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多