Change NameValuePair separator from ‘=’ to ‘:’

Tags: , ,



I am trying to send a HttpPost request, and to do this, from what I understand, you do this:

            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();
            } 

The problem I’m having is that the entity looks like this:

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

But due to the way the server is set up, I need it to look like this:

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

You will notice that rather than equal (=) signs, there are colons (:) separating the key/value pairs.

My is question is: How do I fix this?

Answer

You might consider using JSONObject instead of UrlEncodedFormEntity — since it looks like you want a JSON string, not a URL encoded string.



Source: stackoverflow