gpt4 book ai didi

java - Android HTTP POST 参数发送但服务器未收到

转载 作者:行者123 更新时间:2023-12-01 13:24:21 29 4
gpt4 key购买 nike

API调用

    HttpUriRequest httpRequest = null;
HttpPost request = new HttpPost(url);

String postParameters = "email=" + email + "&name=" + name;

StringEntity stringEntity = new StringEntity(postParameters, "UTF-8");
request.setEntity(stringEntity);

httpRequest = request;

response = client.execute(httpRequest);

我没有在服务器端获取帖子参数。获取参数有效。相同的 API 适用于其他 REST 客户端,但不适用于 Android。

最佳答案

变量httpRequest没有用。变量 postParameters 是字符串吗?

这里是发送带有一些参数的帖子来上传图像的示例。

HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(getString(R.string.url));

try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

builder.addPart("picture", new FileBody(mSharedImagePath, ContentType.create("image/jpeg")));

// Adding params
List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("param1", "utf-8 encoded text");
params.add(new BasicNameValuePair("param2", "another utf-8 encoded text");

for (NameValuePair param : params) {
builder.addTextBody(param.getName(), param.getValue(), ContentType.create("text/plain", Charset.forName("UTF-8")));
}

builder.setCharset(MIME.UTF8_CHARSET);
httppost.setEntity(builder.build());

HttpResponse response = httpclient.execute(httppost);

HttpEntity resEntity = response.getEntity();
resEntity.getContentLength();
//......

} catch (FileNotFoundException e) {
Logger.getInstance().log(e);
} catch (ClientProtocolException e) {
Logger.getInstance().log(e);
} catch (IOException e) {
Logger.getInstance().log(e);
} catch (Exception e) {
Logger.getInstance().log(e);
}
<小时/>

在你的代码中它可以是这样的

HttpPost request = new HttpPost(url);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email","my email"));
nameValuePairs.add(new BasicNameValuePair("name","my name"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

response = client.execute(request);

关于java - Android HTTP POST 参数发送但服务器未收到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852177/

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