由于Android应用,我将从API解析json字符串。当我在网页浏览器中键入URL时,我看到这个json: {" id":12345," title":"华为:Sv�?e?kaoptick�chvl�ken"} 我在我的应用程序中使用相同的字符串,但我不知道如何解析它。这是捷克变音符号。 我方法的代码在这里:
public static String getJSONString(String url) {
String jsonString = null;
HttpURLConnection linkConnection = null;
try {
URL linkurl = new URL(url);
linkConnection = (HttpURLConnection) linkurl.openConnection();
linkConnection.setRequestMethod("GET");
String userCredentials = "xxx:yyyyy";
linkConnection.setRequestProperty("Authorization", "Basic " + new String(Base64.encode(userCredentials.getBytes(), Base64.NO_WRAP)));
linkConnection.setRequestProperty( "Accept-Encoding", "utf-8" );
int responseCode = linkConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream linkinStream = linkConnection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int j = 0;
while ((j = linkinStream.read()) != -1) {
baos.write(j);
}
byte[] data = baos.toByteArray();
jsonString = new String(data, "utf-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (linkConnection != null) {
linkConnection.disconnect();
}
}
return jsonString;
}
感谢您提前。