Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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 接收POST时出现错误404_Php_Json_Post - Fatal编程技术网

Php 接收POST时出现错误404

Php 接收POST时出现错误404,php,json,post,Php,Json,Post,我需要用php从android向web服务器发送json帖子。我尝试了很多代码,但都不管用 现在,我尝试了一个简单的邮政与邮递员,有和没有发送数据。并且总是收到404错误。如果我发送数据与得到的页面工作良好 如果要测试,请参阅php: 网站: 编辑 <?php $servername="localhost"; $username="root"; $password=""; $db="derar"; $connection=mysqli_connect($servername,$us

我需要用php从android向web服务器发送json帖子。我尝试了很多代码,但都不管用

现在,我尝试了一个简单的邮政与邮递员,有和没有发送数据。并且总是收到404错误。如果我发送数据与得到的页面工作良好

如果要测试,请参阅php:

网站:


编辑

<?php 
 $servername="localhost";
$username="root";
$password="";
$db="derar";
$connection=mysqli_connect($servername,$username,$password,$db);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$movie_name=$obj['movie_name'];
mysqli_query($connection,"insert into  movie (movie_id, movie_name) VALUES (NULL,'$movie_name');");
echo "inserted";
?>

404错误:

POST/androidphp/index.php HTTP/1.1主机:gclimb.com缓存控制:无缓存邮差令牌:de575030-0343-64d9-fce3-e640ce12780c内容类型:多部分/表单数据;边界=----WebKitFormBoundary7MA4YWxkTrZu0gW----获取404错误

工作好:

GET/androidphp/index.php HTTP/1.1 主持人:gclimb.com 缓存控制:没有缓存
邮递员令牌:f2999796-9338-6ef6-9877-075be9a8e530当发出
邮政请求时,不要忘记请求路径区分大小写

尝试使用此目标执行您的请求:

http://gclimb.com/androidphp/index.php

当发出
POST请求时
,不要忘记请求路径区分大小写

尝试使用此目标执行您的请求:

http://gclimb.com/androidphp/index.php

在这里,我将提供一个完整的PHP和一个android应用程序,将数据从android应用程序发送到服务器

假设数据库中有此表:

------------------------
| movie_id | movie_name|
------------------------
1-PHP脚本

<?php 
 $servername="localhost";
$username="root";
$password="";
$db="derar";
$connection=mysqli_connect($servername,$username,$password,$db);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$movie_name=$obj['movie_name'];
mysqli_query($connection,"insert into  movie (movie_id, movie_name) VALUES (NULL,'$movie_name');");
echo "inserted";
?>

main活动

public class MainActivity extends AppCompatActivity {
    private  Button send_button;
    private String Server_URL="http://192.168.1.101/derar/insertDataToServer.php";
    private String movie_name="Titanic";

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

        send_button=(Button)findViewById(R.id.send_button);// Button Assignment
        send_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendDataToServer sendDataToServer=new SendDataToServer();
                sendDataToServer.execute(Server_URL,movie_name);


            }
        });// Button On Click Listener
    }


    private class SendDataToServer extends AsyncTask<String, Void, Boolean> {

        @Override
        protected Boolean doInBackground(String... urls) {

            OutputStream os = null;
            InputStream is = null;
            HttpURLConnection conn = null;


            try {

                URL url = new URL(urls[0]);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("movie_name", urls[1]);
                String message = jsonObject.toString();
                Log.d(message, "Test");
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setFixedLengthStreamingMode(message.getBytes().length);

                conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
                conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");

                conn.connect();

                os = new BufferedOutputStream(conn.getOutputStream());
                os.write(message.getBytes());

                os.flush();

                is = conn.getInputStream();


            } catch (MalformedURLException e) {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                ;
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                ;
                e.printStackTrace();
                return false;
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {

                try {
                    assert os != null;
                    os.close();
                    assert is != null;
                    is.close();

                } catch (IOException e) {

                    e.printStackTrace();
                }

                conn.disconnect();
            }


            return true;
        }


        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {


                  Toast.makeText(getApplicationContext(), "لقد نجح إرسال المعلومات", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "فشل في إرسال المعلومات", Toast.LENGTH_LONG).show();

            }

        }
    }
}
public类MainActivity扩展了AppCompatActivity{
私人按钮发送按钮;
专用字符串服务器\u URL=”http://192.168.1.101/derar/insertDataToServer.php";
私人字符串电影_name=“泰坦尼克号”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
发送按钮=(按钮)findViewById(R.id.send按钮);//按钮分配
send_button.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
SendDataToServer SendDataToServer=新建SendDataToServer();
执行(服务器URL、电影名称);
}
});//单击侦听器上的按钮
}
私有类SendDataToServer扩展异步任务{
@凌驾
受保护的布尔doInBackground(字符串…URL){
OutputStream os=null;
InputStream=null;
HttpURLConnection conn=null;
试一试{
URL=新URL(URL[0]);
JSONObject JSONObject=新的JSONObject();
jsonObject.put(“电影名称”,URL[1]);
字符串消息=jsonObject.toString();
日志d(消息“测试”);
conn=(HttpURLConnection)url.openConnection();
连接设置读取超时(10000);
连接设置连接超时(15000);
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
conn.setFixedLengthStreamingMode(message.getBytes().length);
conn.setRequestProperty(“内容类型”,“应用程序/json;字符集=utf-8”);
conn.setRequestProperty(“X-request-With”,“XMLHttpRequest”);
连接();
os=新的BufferedOutputStream(conn.getOutputStream());
write(message.getBytes());
os.flush();
is=conn.getInputStream();
}捕获(格式错误){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
;
e、 printStackTrace();
返回false;
}捕获(IOE异常){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
;
e、 printStackTrace();
返回false;
}捕获(JSONException e){
e、 printStackTrace();
}最后{
试一试{
断言os!=null;
os.close();
断言为!=null;
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
连接断开();
}
返回true;
}
@凌驾
受保护的void onPostExecute(布尔结果){
如果(结果){
Toast.makeText(getApplicationContext(),“Toast.LENGTH”show();
}否则{
Toast.makeText(getApplicationContext(),“Toast.LENGTH”.show();
}
}
}
}
别忘了做:

1-将此权限添加到清单文件:

<uses-permission android:name="android.permission.INTERNET" />

2-更改此
服务器的IP地址\u URL=”http://192.168.1.101/derar/insertDataToServer.php";

要运行localhost服务器,请访问您自己的IP地址


我希望这对您有用。

这里我将提供一个完整的PHP和一个android应用程序,用于将数据从android应用程序发送到服务器

假设数据库中有此表:

------------------------
| movie_id | movie_name|
------------------------
1-PHP脚本

<?php 
 $servername="localhost";
$username="root";
$password="";
$db="derar";
$connection=mysqli_connect($servername,$username,$password,$db);
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
$movie_name=$obj['movie_name'];
mysqli_query($connection,"insert into  movie (movie_id, movie_name) VALUES (NULL,'$movie_name');");
echo "inserted";
?>

main活动

public class MainActivity extends AppCompatActivity {
    private  Button send_button;
    private String Server_URL="http://192.168.1.101/derar/insertDataToServer.php";
    private String movie_name="Titanic";

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

        send_button=(Button)findViewById(R.id.send_button);// Button Assignment
        send_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendDataToServer sendDataToServer=new SendDataToServer();
                sendDataToServer.execute(Server_URL,movie_name);


            }
        });// Button On Click Listener
    }


    private class SendDataToServer extends AsyncTask<String, Void, Boolean> {

        @Override
        protected Boolean doInBackground(String... urls) {

            OutputStream os = null;
            InputStream is = null;
            HttpURLConnection conn = null;


            try {

                URL url = new URL(urls[0]);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("movie_name", urls[1]);
                String message = jsonObject.toString();
                Log.d(message, "Test");
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setFixedLengthStreamingMode(message.getBytes().length);

                conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
                conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");

                conn.connect();

                os = new BufferedOutputStream(conn.getOutputStream());
                os.write(message.getBytes());

                os.flush();

                is = conn.getInputStream();


            } catch (MalformedURLException e) {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                ;
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
                ;
                e.printStackTrace();
                return false;
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {

                try {
                    assert os != null;
                    os.close();
                    assert is != null;
                    is.close();

                } catch (IOException e) {

                    e.printStackTrace();
                }

                conn.disconnect();
            }


            return true;
        }


        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {


                  Toast.makeText(getApplicationContext(), "لقد نجح إرسال المعلومات", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "فشل في إرسال المعلومات", Toast.LENGTH_LONG).show();

            }

        }
    }
}
public类MainActivity扩展了AppCompatActivity{
私人按钮发送按钮;
专用字符串服务器\u URL=”http://192.168.1.101/derar/insertDataToServer.php";
私人字符串电影_name=“泰坦尼克号”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
发送按钮=(按钮)findViewById(R.id.send按钮);//按钮分配
send_button.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
SendDataToServer SendDataToServer=新建