I have 2 same projects that has same task, it is want to access web service from android with JsonReader,
project 1 was running well and everything good there is no problem, project 2 was appearing the exception ===> "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 47"
for make sure I even copy and paste it class that call JsonReader from proj1 to proj2 can anyone help me to fix it. this is the class I called from 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;
}`