gpt4 book ai didi

java - POST 数据未通过 HttpURLConnection 发送

转载 作者:行者123 更新时间:2023-11-29 08:42:34 24 4
gpt4 key购买 nike

我正在尝试通过 HttpURLConnection 发送 POST 请求,这是代码

public class BackgroundTask extends AsyncTask<String, Void, Void> {
Context context;
Activity activity;
StringBuffer str = null;
int responseCode;
String responseMessage;

public BackgroundTask(Context context) {
this.context = context;
this.activity = (Activity) context;
}

@Override
protected Void doInBackground(String... params) {
HttpURLConnection connection = null;

OutputStream outputStream = null;
InputStream inputStream = null;

BufferedReader reader = null;
BufferedWriter writer = null;

String method = params[1];

if(method.equals("post")) {
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

outputStream = connection.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

String data = URLEncoder.encode(params[2] + "=" + params[3], "UTF-8");
writer.write(data);
responseCode = connection.getResponseCode();
responseMessage = connection.getResponseMessage();
inputStream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
str = new StringBuffer();

String line = "";

while ((line = reader.readLine()) != null) {
str.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null)
connection.disconnect();
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else if(method.equals("get")) {

}
return null;
}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Void aVoid) {
TextView txt = (TextView) activity.findViewById(R.id.txt);
if(str != null)
txt.setText(str.toString());
Toast.makeText(activity, responseMessage, Toast.LENGTH_LONG).show();
}
}

responseCode 是 200,这意味着一切正常,但是它显示 Undefined index: id

id 在 php 文件中定义明确

$user = User::find_by_id($_POST['id']);
echo json_encode($user);

当我从 html 文件发送发布请求时它工作正常,但是当我从应用程序发送它时它说 id undefined 这意味着没有发送 POST 数据。

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BackgroundTask myTask = new BackgroundTask(MainActivity.this);
myTask.execute(link, "post", "id", "5");
}
});

这就是我在主 Activity 中实例化 asynctask 对象的方式

更新:当我发送未编码的字符串时,它工作正常!writer.write("id=5");//完美运行!我在代码中使用的 URLEncoder 有什么问题?

最佳答案

我相信你在这行有问题:

String data = URLEncoder.encode(params[2] + "=" + params[3], "UTF-8");

您正在对 = 以及参数进行 url 编码,这就是服务器无法识别表单字段的原因。尝试仅对参数进行编码:

String data = URLEncoder.encode(params[2], "UTF-8") + "=" + URLEncoder.encode(params[3], "UTF-8");

原因是 URL 编码是为了在值(或键)中传递特殊字符,如 =。基本上,在进行解码之前,服务器将使用 &= 拆分和解析键值对。当您对 = 字符进行 url 编码时,服务器在拆分和解析阶段根本无法识别它。

关于java - POST 数据未通过 HttpURLConnection 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38985611/

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