2013-09-21 46 views
2

我試圖發送一個HttpPost要求,而要做到這一點,從我的理解,你這樣做:變化的NameValuePair分體「:」

  HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(uri[0]); 
      try { 
       List<NameValuePair> nvp = new ArrayList<NameValuePair>(); 
       nvp.add(new BasicNameValuePair("{\"UserName\"", "\"michigan\"")); 
       nvp.add(new BasicNameValuePair("\"Password\"", "\"fanaddicts\"")); 
       nvp.add(new BasicNameValuePair("\"DeviceHarwareId\"", "\"NW58xfxz/w+jCiI3E592degUCL4=\"")); 
       nvp.add(new BasicNameValuePair("\"DeviceTypeId\"", "\"1\"}")); 
       post.setEntity(new UrlEncodedFormEntity(nvp)); 

       response = httpClient.execute(post); 

       Log.i("Feed Response", "Feed: " + response.getStatusLine().getStatusCode()); 

      } catch (UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

我有問題是實體看起來是這樣的:

[{"UserName"="michigan", "Password"="fanaddicts", "DeviceHarwareId"="NW58xfxz/w+jCiI3E592degUCL4=", "DeviceTypeId"="1}] 

但由於服務器設置的方式,我需要它看起來像這樣:

[{"UserName":"michigan", "Password":"fanaddicts", "DeviceHarwareId":"NW58xfxz/w+jCiI3E592degUCL4=", "DeviceTypeId":"1}] 

你會發現,而不是等號(=)的標誌,有冒號(:)分隔鍵/值對。

我的是問題是:我該如何解決這個問題?

+1

考慮使用[JSONObject的(http://developer.android.com/reference/org/json/JSONObject.html),而不是UrlEncodedFormEntity - 因爲它看起來像你想有一個JSON字符串,而不是一個URL編碼的字符串。 – jedwards

+1

@jedwards:你爲什麼不把它寫成答案? – gunar

+0

@jedwards使您的評論的答案,我會接受的。有效。好想法。謝謝。 – BlackHatSamurai

回答

1

您可以考慮使用JSONObject代替UrlEncodedFormEntity的 - 因爲它看起來像你想有一個JSON字符串,而不是一個URL編碼的字符串。