2011-12-04 39 views
4

我開發一個Android應用程序中,我必須做一個POST請求到服務器的參數,在shell的要求應該是這樣的:POST請求的Android與JSON

捲曲-v -H「接受:application/xml「-H」內容類型: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「 : 「2027年11月28日十三點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(); 
          } 

它可以完美的從命令外殼,但是當從手機發送請求時,我沒有授權

任何幫助將不勝感激。

謝謝!

我做了什麼一個很好的用戶建議我,這裏是我的了:

POST/MARCAS HTTP/1.1 的Content-Length:300 的Content-Type:text/plain的;字符集= ISO-8859-1 內容類型:應用/ JSON 主機:> HOSTNAME> 連接:保持活動 用戶代理:Apache的HttpClient的/不可用(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 

這很奇怪的是,「\」出現在發佈請求,同時,與會的代碼......他們來自一個字符串,是應該忽略他們。不是嗎?

還是完全失明在這個問題...

+0

嘗試使用JSONObject構建marca而不是將字符串附加在一起 –

+0

@Chunhui我試過並得到了相同的結果...... :(但現在參數中的順序改變了......它是否改變什麼? – vicks630

回答

0

就是這樣,錯誤是:

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

服務器只接受字符串...它被解析爲一個int。

謝謝大家! :)