我可以编译项目并很好地解析数据,但我的日志消息显示
Error:com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON
和
Error:com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 186
我不知道为什么会这样。
我试着找到一些答案,仍然找不到方法。
这是解析json数据的代码,我导入import com.google.gson.JsonObject;
:
@Override
protected String doInBackground(String... params) {
String url = params[0];
String UD_MBID = params[1];
String UD_TestCode = params[2];
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("UD_MBID", UD_MBID);
jsonObject.addProperty("UD_TestCode", UD_TestCode);
try {
String routeJson = getRemoteData(url, jsonObject.toString());
return routeJson;
} catch (IOException ex) {
Log.d(TAG, ex.toString());
return null;
}
}
private String getRemoteData(String url, String jsonOut) throws IOException {
StringBuilder jsonIn = new StringBuilder();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("charset", "UTF-8");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
bw.write(jsonOut);
Log.d(TAG, "jsonOut" + jsonOut);
bw.close();
String line;
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
jsonIn.append(line);
}
} else {
Log.d(TAG, "responseCode" + responseCode);
}
connection.disconnect();
Log.d(TAG, "jsonIn" + jsonIn);
return jsonIn.toString();
}
我可以在任何地方设置setLenient(true)
吗?
有什么想法吗?提前谢谢。