我有 2 个具有相同任务的相同项目,它想使用 JsonReader 从 android 访问 Web 服务,
项目 1运行良好,一切正常,没有问题, 项目 2出现异常 ===>“com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY 但在第 1 行第 47 列是 BEGIN_OBJECT”
为了确保我什至复制并粘贴了从 proj1 到 proj2 调用 JsonReader 的类,任何人都可以帮我修复它。这是我从 Asyntasks 调用的类
public <T> T Gets(String url, List<NameValuePair> params, final Type t) {
HttpURLConnection connection = null;// getNewHttpconnection();
// String paramString = URLEncodedUtils.format(params, "utf-8");
if(params != null) {
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
}
try {
URL urls = new URL(url);
connection = (HttpURLConnection) urls.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "text/html");
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setReadTimeout(60000);
connection.setConnectTimeout(60000);
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.e("asds", "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage());
return null;
}
else
{
int fileLength = connection.getContentLength();
InputStream inputStream = connection.getInputStream();
String contentEncoding = connection.getContentEncoding();
if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
inputStream = new GZIPInputStream(inputStream);
}
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "UTF-8"));
reader.setLenient(true);
System.out.print(reader);
Gson gson = new GsonBuilder().create();
T temp = gson.fromJson(reader, t);
// tem = temp;
inputStream.available();
inputStream.close();
return temp;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ClientProtocolException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}`