Reputation: 189
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>
Upvotes: 0
Views: 301
Reputation: 5006
Based on the fact that the string does not start with a '{' character I would say the json you have is actually invalid and malformed json. Take a look here and here.
I would say your options are to see if you can get the malformed json fixed on the server side, or else on the client do some checks to see if it's malformed and try to fix it before passing the string to the jsonObject parsers.
Upvotes: 0
Reputation: 5267
Looks to me like the issue is with the page you have created to return the response. Is there a particular reason you are using an HTML page with frames? If you browse the page loaded in the frame (http://88.198.1.116:9080/parentconnect/services/student/getStudentDetails?studentid=1&schoolid=1) you will see that the source has the JSON string you are after.
Why are you not browsing to this URL instead of the HTML page?
Upvotes: 1