Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 服务器端未收到使用httppost的过帐_Php_Android_Http Post_Httpclient - Fatal编程技术网

Php 服务器端未收到使用httppost的过帐

Php 服务器端未收到使用httppost的过帐,php,android,http-post,httpclient,Php,Android,Http Post,Httpclient,我不熟悉Android编程,但对PHP有相当的了解 我正试图通过post请求将数据发送到我的服务器。我有以下代码: ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("year","1980")); HttpClient httpclient = new DefaultHttpClient()

我不熟悉Android编程,但对PHP有相当的了解

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

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);
ArrayList nameValuePairs=新的ArrayList();
nameValuePairs.add(新的基本nameValuePairs(“年份”,“1980”));
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(nameValuePairs,“UTF-8”);
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
InputStream inps=entity.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(inps));
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
某人附加(行);
}
inps.close();
结果=sb.toString();
Log.v(“收到的数据”,结果);
我在互联网上的许多地方都找到了代码

在PHP方面,我写了以下内容:

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

但我只得到“某物”(在日志值“data received”中)作为我应用端的输出


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

$\u请求
并不总是受支持,请根据参数所在的位置使用
$\u GET
$\u POST

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


                }

在代码中已经是一个检查,成功时他会给出一个toast消息和一个日志。在php服务器上,您可以使用
print\r($\u POST)这应该没有问题。您现在使用的代码是什么?

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

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

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

有人知道它为什么工作吗?

你能记录你发送的请求吗?可能您在构造请求时犯了错误。我还尝试了print_r(var_dump($_request))、print_r(var_dump($_POST))和print_r(var_dump($_GET))……它的返回数组(0){}我发送的数据是前两行……我还需要发送其他内容吗?我的意思是,如果可以,显示原始HTTP请求。如果HTTP请求是好的,那么问题在于接收器。如果HTTP请求不正确,则发送方会出现问题。
httppost.getRequestLine()
?我在这里猜。另外,也许您应该使用
setParams()
而不是
setEntity
?内容类型设置为什么?需要特定的PHP才能确认。我也添加了内容类型。仍然不工作…请确保您的内容类型设置为“application/x-www-form-urlencoded”,并且您的内容长度也已设置。我对javaapi还不太了解,所以不能说它是否是为您设置的。在服务器端脚本中,执行以下操作:“print_r($\u server);print_r($\u POST);”。这将转储标题并将数据发布到屏幕,以便您可以直观地检查结果。让我知道你发现了什么。嘿,伙计,你是为DH4做宝石的Shubham吗?;)什么是getApplicationContext()?@Shubham它只是一个获取应用程序上下文的函数。它有时在函数中给出,但这是一个解决方法,如果它不是…我尝试了这个代码…它仍然不工作…服务器有问题吗?你在模拟器中尝试吗?因为我记得我也有问题。我试过了…给了我一个空数组…请检查解决方案…你能解释一下为什么会这样吗?你现在使用的代码是什么?因为发布的代码对我有用。你发送的数据不是有问题吗?