У меня есть 2 одинаковых проекта с одинаковой задачей, я хочу получить доступ к веб-сервису с Android с помощью JsonReader,
проект 1 работал хорошо, и все хорошо, проблем не возникало, проект 2 появлялся с исключением ===> "com.google.gson.JsonSyntaxException: java.lang. IllegalStateException: ожидалось BEGIN_ARRAY, но было BEGIN_OBJECT в строке 1, столбце 47"
чтобы убедиться, что я даже копирую и вставляю класс, который вызывает JsonReader из proj1 в proj2, может ли кто-нибудь помочь мне это исправить. это класс, который я вызвал из 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;
}`
Type t
не соответствует JSON для десериализации. - person Vladyslav Matviienko   schedule 01.02.2017