gpt4 book ai didi

php - 使用 Android 发布变量

转载 作者:行者123 更新时间:2023-11-30 00:03:44 25 4
gpt4 key购买 nike

我使用我的代码在 MySQL 中上传数据。

        HttpPost httppost = new HttpPost(URL_POST_TIENDAS);
HttpClient client = new DefaultHttpClient();

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", json));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
// Log.e(TAG, "Ejecutando POST: Mandando tiendas");
HttpResponse httpResponse = client.execute(httppost);
if (httpResponse != null) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
message = NetworkUtils.Entity2String(httpResponse);
Log.e(TAG, "Respuesta del Post Tienda:" + message);
}
} else {
Err error = new Err(statusCode, message, "upload_tiendas");
MyApplication.lErrors.add(error);
this.cancel(true);
}

此代码给我一个 500 错误

在 PHP 中,我通过 $_REQUEST 接收变量,因此当我调试应用程序时,复制 json 变量并将其放入完整 URL 中,没有问题。

这表明我的 json 变量正常,如 URL_POST_TIENDAS。

为什么使用POST会出现问题???这不是我第一次发现这个问题。我总是将其更改为 GET,但这一次,我想了解为什么会失败,因为我可能有很多信息要上传,所以 GET 不太合适!

编辑:当查看日志服务器时,我没有看到任何有关 500 错误的信息。

编辑2:httpost:

httppost    HttpPost  (id=830032727152) 
aborted false
abortLock ReentrantLock (id=830032727328)
connRequest null
entity UrlEncodedFormEntity (id=830032731528)
chunked false
content (id=830032746160)
[0...99]
[100...199]
[200...299]
[300...399]
[400...499]
[500...599]
[600...699]
[700...724]
contentEncoding null
contentType BasicHeader (id=830032747320)
headergroup HeaderGroup (id=830032727200)
headers ArrayList (id=830032727216)
array Object[16] (id=830032727240)
modCount 0
size 0
params BasicHttpParams (id=830032789608)
parameters null
releaseTrigger SingleClientConnManager$ConnAdapter (id=830032798576)
uri URI (id=830032727376)

任何帮助将不胜感激!

最佳答案

在 httpPost 中设置内容类型

httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

更新

这是做同样事情的博客。

Send data as json from android to a PHP server

关于php - 使用 Android 发布变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24787757/

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