简体   繁体   English

POST请求带有json参数的Android

[英]POST request Android with parameters in json

I'm developing an Android app in which I have to do a POST request to a server, the request in a shell should be like this: 我正在开发一个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 curl -v -H“Accept:application / xml”-H“Content-type:application / json”-X POST -d'{“marca”:{“user_id”:“78”,“lugar_id”:“2” “蒂恩波”: “二十时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

Well, my Java code looks like: 好吧,我的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();
                            }  

It works perfectly from the command shell, but when sending the request from the phone, I get NOT AUTHORIZED 它在命令shell中运行良好,但是当从手机发送请求时,我得不到授权

Any help would be much appreciated. 任何帮助将非常感激。

Thank you! 谢谢!

I've done what a nice user suggested me and here's what I got: 我做了一个很好的用户建议我,这是我得到的:

POST /marcas HTTP/1.1 Content-Length: 300 Content-Type: text/plain; POST / marcas HTTP / 1.1 Content-Length:300 Content-Type:text / plain; charset=ISO-8859-1 Content-Type: application/json Host: >HOSTNAME> Connection: Keep-Alive User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4) Expect: 100-Continue charset = ISO-8859-1 Content-Type:application / json主机:> HOSTNAME>连接:Keep-Alive User-Agent:Apache-HttpClient / UNAVAILABLE(java 1.4)期望:100-Continue

HTTP/1.1 100 Continue 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

It's weird that the "\\" appear in the post request, while, attending to the code... they come from a String, that is supposed to ignore them... isn't it? 奇怪的是,“\\”出现在帖子请求中,而参加代码......他们来自一个字符串,应该忽略它们......不是吗?

Still completely blind in this issue... 在这个问题上仍然完全失明......

That's it, the error was in: 就是这样,错误发生在:

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

The server only accepted Strings... and it was parsed as an int. 服务器只接受字符串...并将其解析为int。

Thank you everyone! 谢谢大家! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM