Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将NameValuePair分隔符从'=';至';:';_Java_Android_Httprequest - Fatal编程技术网

Java 将NameValuePair分隔符从'=';至';:';

Java 将NameValuePair分隔符从'=';至';:';,java,android,httprequest,Java,Android,Httprequest,我正试图发送一个HttpPost请求,为此,据我所知,您可以这样做: HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(uri[0]); try { List<NameValuePair> nvp = new ArrayList<NameValuePair>()

我正试图发送一个
HttpPost
请求,为此,据我所知,您可以这样做:

            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}]
您会注意到,不是等号(=),而是冒号(:)分隔键/值对


我的问题是:如何修复这个问题?

你可以考虑用UrlEncodedFormEntity来代替它——因为它看起来像是想要一个JSON字符串,而不是一个URL编码的字符串。

< P>一个更好的方法是将JSON字符串序列化成字典。json结构数据无法通过字典索引访问。

考虑使用而不是UrlEncodedFormEntity——因为看起来您需要的是json字符串,而不是URL编码的字符串。@jedwards:为什么不将此作为答案编写?@jedwards将您的评论作为答案,我接受。成功了。好主意。谢谢。@Blaine:太好了,很高兴能帮上忙