【发布时间】:2015-01-31 01:51:22
【问题描述】:
我想从服务器获取数据,但字符串是编码,所以我无法获取编码字符串。那么如何在解码中转换并获取呢?
我能够在没有编码的情况下成功地从服务器获取数据意味着普通字符串。
例子,
desc="PHA+PHN0cm9uZz48YSBocmVmPSJodHRwOi8vd3d3Lm1hcnlzY2VudGVyLm9yZy9jb250ZW50L2hlYWx0aC1jYXJlLXNlcnZpY2VzIj48c3Ryb25nPk1lZGljYWwgU2VydmljZXM8L3N0cm9uZz48L2E+OiB0byBtYWtlIGFu";
我的代码是,
public class JSONFunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
// BufferedReader reader = new BufferedReader(new
// InputStreamReader(is, HTTP.UTF_8), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
【问题讨论】:
-
你的 json 响应是什么:
-
{ 响应:“成功”,数据:[ { s_id:“5”,ser_id:“11”,ser_title:“服务”,标题:“服务”,desc:“PHN0cm9uZz48YSB”}] }