我正在开发的 Android 应用程序中运行哈希表。当我运行该 Activity 时,它会将“ht”的内容转换为 ASCII 字符。有谁知道防止这种情况发生的方法吗?
基本上,我需要对编码文本进行解码并将结果用于字符串。为了工作,字符串中使用的 URL 不能包含编码文本。
哈希表
@SuppressWarnings("unchecked")
private void executeAsyncTask(){
Hashtable<String,String> ht=new Hashtable<String,String>();
GetDeptAyncTask async=new GetDeptAyncTask();
ht.put("?TYPE=leagueSearch&JSON=1","&SEARCH=test");
try {
URLDecoder.decode(""+ht, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@SuppressWarnings("rawtypes")
Hashtable[] ht_array={ht};
async.execute(ht_array);
}
尝试在字符串中使用它
protected String doInBackground(Hashtable<String,String>... params) {
@SuppressWarnings("rawtypes")
Hashtable ht=params[0];
@SuppressWarnings("unchecked")
String json=HelperHttp.getJSONResponseFromURL(BaseUrl+"2013/export", ht);
“json”字符串中“ht”的读取方式
?%3FTYPE%3DleagueSearch%26JSON%3D1=%26SEARCH%test
应该如何读取 URL
?TYPE=leagueSearch&JSON=1&SEARCH=test
“ht”的 LogCat
?:??: W/?(?): Hashtable {?TYPE=leagueSearch&JSON=1=&SEARCH=test}
“json”字符串的 LogCat
?:??: W/?(?): URL==>HIDDENURL/export?%3FTYPE%3DleagueSearch%26JSON%3D1=%26SEARCH%test
?:??: W/?(?): Json Response==><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">....
?:??: W/?(?): at org.json.JSONObject.<init>(JSONObject.java:127)
?:??: W/?(?): at myapp.app.MainActivity$GetDeptAyncTask.parseJsonString(MainActivity.java:80)
最佳答案
您可以使用java.net.URLDecoder
对编码后的文本进行解码
URLDecoder.decode("%3FTYPE%3DleagueSearch%26JSON%3D1=%26SEARCH%3Dtest", "UTF-8")
会产生
?TYPE=leagueSearch&JSON=1=&SEARCH=test
关于java - 防止哈希表转换为 ASCII,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16420817/