收到POST时出现错误404 [英] Error 404 when receive POST
问题描述
我需要使用android将json POST从android发送到Web服务器.我尝试了很多代码,但没有用.
I need send a json POST from android to web server with php. I try a lot of codes but don't works.
现在,我尝试使用带或不带发送数据的Postman进行简单的POST.并始终收到404错误.如果我使用GET发送数据,则页面工作正常.
Now I try a simply POST with Postman, with and without send data. And always receive a 404 error. If I send data with GET the page work fine.
请参阅php,以了解是否要测试:
See the php, for if you want to test:
网站: http://gclimb.com/Androidphp/index.php
<?php
$json = file_get_contents('php://input');
$obj = json_decode($json);
echo $obj["username"];
echo $obj["pass"];
if ($_POST["username"]) {
echo $_POST["username"];
}
if ($_GET["username"]) {
echo $_GET["username"];
}
?>
编辑
404错误:
POST/androidphp/index.php HTTP/1.1主机:gclimb.com缓存控制:无缓存邮递员令牌:de575030-0343-64d9-fce3-e640ce12780c内容类型:multipart/form-data; boundary = ---- WebKitFormBoundary7MA4YWxkTrZu0gW ----获取404错误
POST /androidphp/index.php HTTP/1.1 Host: gclimb.com Cache-Control: no-cache Postman-Token: de575030-0343-64d9-fce3-e640ce12780c Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW ---- Get 404 error
工作正常:
GET/androidphp/index.php HTTP/1.1 主持人:gclimb.com 缓存控制:无缓存 邮递员令牌:f2999796-9338-6ef6-9877-075be9a8e530
GET /androidphp/index.php HTTP/1.1 Host: gclimb.com Cache-Control: no-cache Postman-Token: f2999796-9338-6ef6-9877-075be9a8e530
推荐答案
在这里,我将提供完整的PHP和android应用程序,以将数据从android应用程序发送到服务器.
Here I will provide a complete PHP and an android Application to send data from android application to server.
假设您的数据库中有此表:
Suppose you have this table in your database:
------------------------
| 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";
?>
此代码是简单的代码,您必须进行SQL注入.
this code is simple code, you have to put a SQL injection.
2-Android应用程序
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.win7.derar.MainActivity">
<Button
android:id="@+id/send_button"
android:layout_width="match_parent"
android:text="إضغط لإرسال المعلومات"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity
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();
}
}
}
}
别忘了做:
1-将此权限添加到清单文件:
1- add this permission to manifest file :
<uses-permission android:name="android.permission.INTERNET" />
2-从此Server_URL="http://192.168.1.101/derar/insertDataToServer.php";
到您自己的IP地址以运行localhost服务器.
to your own IP address in order to run the localhost server.
我希望这对您有用.
这篇关于收到POST时出现错误404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!