gpt4 book ai didi

php - 将字符串数据发送到服务器 : string is not sent? 上的 PHP 文件

转载 作者:行者123 更新时间:2023-11-30 01:46:42 24 4
gpt4 key购买 nike

我正在尝试使用 POST 和 `GET 将 string 发送到远程服务器上的 PHP 文件,然后 PHP 将字符串到文本文件。

我的代码(如下)编译和运行没有错误,但是当按下提交按钮时,没有任何 Action 。 IE。 toast 不显示,当我检查从服务器上的 PHP 文件写入的 文本文件 时,什么也没有。

我是否遗漏了一些非常明显会导致这不起作用的东西? (注意:“my_url”是故意的,这不是错误)

安卓代码:

public class MainActivity extends AppCompatActivity {

@Bind(R.id.tvTitle)
TextView title;
@Bind(R.id.etName)
EditText name;
@Bind(R.id.etEmail)
EditText email;
@Bind(R.id.etIdea)
EditText idea;
@Bind(R.id.btnSubmit)
Button submit;

String toSubmit = "";
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

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

submit.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {

try{
new MyTask().execute();
}catch(Exception e){
e.printStackTrace();
}
}
});
}

public static long copyLarge(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}

public static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}

private class MyTask extends AsyncTask<String, Void, String>
{
boolean success = false;

@Override
protected String doInBackground(String... params) {

StringBuilder respData = new StringBuilder();
InputStream stream = null;
OutputStream os = null;
HttpURLConnection httpUrlConnection;
URLConnection conn;
URL url;

try {

url = new URL("my_url/recieveString.php");
conn = url.openConnection();
httpUrlConnection = (HttpURLConnection) conn;

httpUrlConnection.setUseCaches(false);
//httpUrlConnection.setRequestProperty("User-Agent", "App");
httpUrlConnection.setConnectTimeout(30000);
httpUrlConnection.setReadTimeout(30000);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setDoOutput(true);
os = httpUrlConnection.getOutputStream();

toSubmit = "test";

stream = new ByteArrayInputStream(toSubmit.getBytes(StandardCharsets.UTF_8));

copy(stream, os);

httpUrlConnection.connect();

int responseCode = httpUrlConnection.getResponseCode();

if (200 == responseCode) {
InputStream is = httpUrlConnection.getInputStream();
InputStreamReader isr = null;
try {
isr = new InputStreamReader(is);
char[] buffer = new char[1024];
int len;
while ((len = isr.read(buffer)) != -1) {
respData.append(buffer, 0, len);
}
} finally {
if (isr != null) {
isr.close();
success = true;
}
}
is.close();
} else {
// use below to get error stream
//inputStream = httpUrlConnection.getErrorStream();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stream.close();
os.flush();

os.close();
} catch (IOException e) {
e.printStackTrace();
}

return "done";
}
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast toast = Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT);

}
}

}

PHP 文件:

<?php 

$stringRecieved=$_GET["toSubmit"];

$file = 'list.txt';

// Write the contents to the file,
file_put_contents($file, $stringRecieved, FILE_APPEND | LOCK_EX);
?>

最佳答案

如果您正在 POST 到服务器,变量将不会在 GET 中。

$stringRecieved = $_POST["toSubmit"];

顺便说一句,提供包装器并测试 POST 是否实际被使用通常是个好主意;清理输入通常是个好主意,确保访问来自有效来源通常是个好主意,而您的 PHP 似乎还没有这样做。

关于php - 将字符串数据发送到服务器 : string is not sent? 上的 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33618210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com