2016-07-15 79 views
0

我有一個JSON字符串編碼URL編碼編碼我的字符串兩次

String strMappingList = [{"Id": "67","AccessType": "2"},{"Id": "1111","AccessType": "2"},{"Id": "1166","AccessType": "2"}] 

當我沒有URL編碼它編碼strMappingList兩次

try { 
    String str = URLEncoder.encode(strMappingList, "utf-8"); 
} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
} 
+0

上面的代碼編碼方法編碼的字符串僅一次O/p爲%5B%7B%27Id%27%3A +%2767%27%2C%27AccessType%27%3A +%272% 27%7D%2C%7B%27Id%27%3A +%271111%27%2C%27AccessType%27%3A +%272%27%7D%2C%7B%27Id%27%3A +%271166%27%2C%27AccessType% 27%3A +%272%27%7D%5D –

回答

1

試試你的字符串文件取碼

如果你從服務器響應細不string.xml使用在string.xml

<string name="urls">[{"Id": "67","AccessType": "2"},{"Id": "1111","AccessType": "2"},{"Id": "1166","AccessType": "2"}]</string> 

代碼

String strMappingList = getResources().getString(R.string.urls); 

try { 
    String str = URLEncoder.encode(strMappingList, "UTF-8"); 
    System.out.println("Strings"+str); 
} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
} 

使用直接

輸出單時間

%5B%7BId%3A + 67%2CAccessType%3A + 2%7D%2C%7BId%3A + 1111%2CAccessType%3A + 2%7D%2C%7BId%3A + 1166%2CAccessType%3A + 2%7D%5D