gpt4 book ai didi

android - POST 请求 Android 带有 json 中的参数

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:57 27 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我必须在其中向服务器发出 POST 请求,shell 中的请求应该是这样的:

curl -v -H "Accept: application/xml"-H "Content-type: application/json"-X POST -d ' {"marca":{"user_id":"78","lugar_id": "2","tiempo":"20:20:20","distancia_recorrida":"300","velocidad_maxima":"20.8", "velocidad_media":"15.8", "desnivel":"100", "aceleracion ":"5.8", "fecha":"2027-11-28 13:32:30"}, "api_token":""}' nameOfSerVer

好吧,我的 Java 代码如下所示:

HttpClient httpclient = new DefaultHttpClient();  
HttpPost httppost = new HttpPost(NameOfServer);

try{
String marca ="{\"user_id\":\""+settings.getInt("user_id",0)+"\",\"lugar_id\":\""+id_lugar+"\",\"tiempo\":\""+tiempo+"\"," +
"\"distancia_recorrida\":\""+distancia+"\",\"velocidad_maxima\":\""+round((b.getDouble("velMax")*3.6),2, BigDecimal.ROUND_UP)+"\"," +
" \"velocidad_media\":" +
"\""+velocidad_media+"\", \"desnivel\":\""+round(b.getDouble("maxDescenso"), 2, BigDecimal.ROUND_UP)+"\"," +
" \"aceleracion\":\""+round(b.getDouble("ac")/9.806, 2, BigDecimal.ROUND_UP)+"\"," +
" \"fecha\":\"2014-11-28 13:32:30\"}";



JSONObject json = new JSONObject();

json.put("marca", marca);
json.put("api_token",settings.getString("api_token", ""));
StringEntity se = new StringEntity(json.toString());

se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);

HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity =response.getEntity();

Log.e("USER", EntityUtils.toString(responseEntity).trim());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e("USER", e.getMessage());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.e("USER", e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("USER", e.getMessage());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

它在命令 shell 中工作得很好,但是当从手机发送请求时,我没有得到授权

如有任何帮助,我们将不胜感激。

谢谢!

我已经完成了一位好用户的建议,这就是我得到的:

POST/马卡斯 HTTP/1.1内容长度:300内容类型:文本/纯文本;字符集=ISO-8859-1内容类型:application/json主持人:>HOSTNAME>连接:保持 Activity 用户代理:Apache-HttpClient/UNAVAILABLE (java 1.4)期望:100-继续

HTTP/1.1 100 继续

{"marca":"{\"user_id\":\"78\",\"lugar_id\":\"3\",\"tiempo\":\"00:14\",\"distancia_recorrida\":\"0.0\",\"velocidad_maxima\":\"0.0\", \"velocidad_media\":\"0.0\", \"desnivel\":\"0.0\", \"aceleracion\":\"0.01\", \"fecha\":\"2014-11-28 13:32:30\"}","api_token":"47fc42ea02a20456d7b901d5b26590a84d0a92d2"}HTTP/1.1 401 Authorization Required
Date: Sun, 04 Dec 2011 02:48:37 GMT
Server: Apache/2.2.9 (Debian) Phusion_Passenger/3.0.2 PHP/5.2.6-1+lenny9 with Suhosin-Patch
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.2
X-UA-Compatible: IE=Edge,chrome=1
X-Runtime: 0.004435
Cache-Control: no-cache
Status: 401
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

e
NOT AUTHORIZED

奇怪的是,“\”出现在 post 请求中,同时注意代码……它们来自一个字符串,应该忽略它们……不是吗?

在这个问题上还是完全瞎了...

最佳答案

就是这样,错误在:

String marca ="{\"user_id\":\""+settings.getInt("user_id",0)

服务器只接受字符串...并且它被解析为一个 int。

谢谢大家! :)

关于android - POST 请求 Android 带有 json 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8372182/

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