I need to parse json object for this url I have used following code
private void parse(String url2) throws MalformedURLException, IOException,JSONException {
// TODO Auto-generated method stub
InputStream is = new URL(url2).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
for(int i=0;i<valArray.length();i++)
{
String p = nameArray.getString(i) + "," + valArray.getString(i);
Log.i("p",p);
}
} finally {
is.close();
}
}
private String readAll(BufferedReader rd) throws IOException {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}}
But I am getting the source of the file in the jsonText String.And as it does not start with a '{' i am getting following error in the log :
org.json.JSONException: A JSONObject text must begin with '{' at character 1>