因此,我正在尝试将android手机中的POST请求发送到服务器(笔记本电脑),它们都在同一网络上。我收到如上所述的错误。我以为这可能与防火墙有关,但是我禁用了它。我打开Firefox控制台,看到此错误
The script from “http://192.168.56.1:8080/upload_db.php” was loaded even though its MIME type (“application/x-httpd-php”) is not a valid JavaScript MIME type.
。
有关更多背景信息,我正在将(db2).db文件从手机上载到服务器。所以这是我用来上传此文件的代码。
public class UploadTaskAsync extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... strings) {
try {
String url = "http://192.168.56.1:8080/upload_db.php";
String charset = "UTF-8";
File dbFile= new File(Environment.getExternalStorageDirectory()+"/databasesFolder/userDB.db");
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
URLConnection connection;
connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-sqlite3; boundary=" + boundary);
try (
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"" + dbFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: application/x-sqlite3; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
writer.append(CRLF).flush();
FileInputStream vf = new FileInputStream(dbFile);
try {
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = vf.read(buffer, 0, buffer.length)) >= 0)
{
output.write(buffer, 0, bytesRead);
}
// output.close();
//Toast.makeText(getApplicationContext(),"Read Done", Toast.LENGTH_LONG).show();
}catch (Exception exception)
{
//Toast.makeText(getApplicationContext(),"output exception in catch....."+ exception + "", Toast.LENGTH_LONG).show();
Log.d("Error", String.valueOf(exception));
publishProgress(String.valueOf(exception));
// output.close();
}
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF).flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Request is lazily fired whenever you need to obtain information about response.
int responseCode = ((HttpURLConnection) connection).getResponseCode();
System.out.println(responseCode); // Should be 200
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... text) {
Toast.makeText(getApplicationContext(), "In Background Task " + text[0], Toast.LENGTH_LONG).show();
}
}
这是我正在使用的后端代码,如下所示:
<?php
$file_path = "C:\\Users\\<username>\\Documents\\";
$file = $file_path . basename($_FILES['uploaded_file']['name']);
if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file)) {
echo "Uploaded";
} else {
echo "fail";
}
?>
对于服务器设置,我正在使用npm http-server。这是因为我的服务器未在端口8080上侦听还是在android应用程序端?如果有人能指出我正确的方向,那就太好了。这个看似简单的任务占用了太多时间。据我了解,当我使用http-server .
启动服务器时,服务器不需要任何身份验证即可接受发布请求。服务器在电话浏览器上可以正常打开,我可以看到已编写的index.html。因此,我知道服务器可以运行。但是,我不知道我在移动应用程序部分出了什么问题。
答案 0 :(得分:0)
JS(NPM)HTTP服务器不执行PHP脚本。您必须设置臭名昭著的WAMP堆栈,以便在通过URL调用PHP脚本时执行该脚本。
Setting Up Apache/MySQL/PHP (AMP) on Linux (LAMP), Windows (WAMP) and Mac OS (MAMP)
另一方面,您还可以使用ADB Reverse将端口从设备转发到笔记本电脑。