How to get integer from json object async in android? - android

Original Question -
I was following a tutorial on json in android, but having problem to get json value using Async in android. First I created jsonparser class and added following to it -
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
// Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
// Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
Then to use this in fragment by async i tried -
class getTopicId extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Topic of the Day ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl("http://medicalguru.in/android/tod.php");
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
topic_id = json.getInt("value");
}
catch (JSONException e) {
e.printStackTrace();
}
finally {
Toast.makeText(getActivity(), "topic id -"+topic_id, Toast.LENGTH_LONG).show();
}
}
}
but the app is crashing when i execute this async.
my logcat shows -
03-06 15:07:03.123: E/AndroidRuntime(2041): FATAL EXCEPTION: main
03-06 15:07:03.123: E/AndroidRuntime(2041): java.lang.NullPointerException
03-06 15:07:03.123: E/AndroidRuntime(2041): at in.medicalguru.DailyTestFragment$getTopicId.onPostExecute(DailyTestFragment.java:786)
03-06 15:07:03.123: E/AndroidRuntime(2041): at in.medicalguru.DailyTestFragment$getTopicId.onPostExecute(DailyTestFragment.java:1)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.os.Looper.loop(Looper.java:137)
03-06 15:07:03.123: E/AndroidRuntime(2041): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-06 15:07:03.123: E/AndroidRuntime(2041): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 15:07:03.123: E/AndroidRuntime(2041): at java.lang.reflect.Method.invoke(Method.java:525)
03-06 15:07:03.123: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-06 15:07:03.123: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-06 15:07:03.123: E/AndroidRuntime(2041): at dalvik.system.NativeStart.main(Native Method)
The link mentioned above in the code outputs as -
{"value":1}
when opened normally, but when I run in async it says err related to nullpointer?
Couldn't figure, where I am making mistake ?
Edit 1 :
*tod.php* code (Since Matthew has pointed something which I dont understand, so it may be helpful)
<?php
$today = date("Ymd");
switch($today){
case 20140304 :
$tod["value"] = 24;
break;
case 20140305 :
$tod["value"] = 25;
break;
case 20140306 :
$tod["value"] = 1;
break;
default:
$tod["value"] = 1;
break;
}
echo json_encode($tod);
?>
Edit 2 : *Found the problem (fully for me) but Partly in Real* - A special thanks to Matthew for his idea, to uncomment the error logs of function getJSONFromUrl(String url). I got a new error line in logcat -
Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
After little bit of search on google and stacksoverflow I found one suggestion to change chrSet to UTF-8 instead of iso-8859-1. and it worked for my requested url in both situations (with or without WWW - suggestion to test, by nikis). But this answer is partial real, because, now 2 unanswered questions developed -
1. Why did chrSet change worked? In future, what chrSet to be used, to avoid this problem, and what's the use of other one chrSet?
2. Matthew has replicated my Json to his server mwesly.com/test/ . Trial on his server, logcat shows following error in all cases (with or without WWW, with or without using index.php in the end) -
Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
Now, why is this error appearing on his server? What should be done to prevent/treat this error.
If I need to ask these updated questions separately, please let me know.

It looks like json is null on following line, which is giving you the NullPointerException.
topic_id = json.getInt("value");
I think this is because your parser is having issues parse the json. I wrote a quick python script and tried to parse the page and it failed as well. For some reason, the response I get from http://medicalguru.in/android/tod.php is:
\xef\xbb\xbf{"value":1}
I am not sure why the \xef\xbb\xbf bytes are there, but they are likely causing the problem.
edit: a quick google search says it is the UTF-8 BOM. If you remove it your android code should work.
edit2: I put an example json on http://mwesly.com/test. Try hitting that json to see if your android code is correct. If it works, it means there is a problem with your endpoint, not your android code.
edit3: example of using loopj:
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://mwesly.com/test", null, new JsonHttpResponseHandler() {
#Override
public void onSuccess(JSONObject json) {
// do something with the json
int value = json.getInt("value");
}
});
I'm going to head to bed, if you still haven't solved this problem when I wake up, I'll give it another look. Good luck and happy coding!

Try to use the following getJSONFromUrl method:
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
return new JSONObject(EntityUtils.toString(httpResponse.getEntity()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

private JSONObject jObj = null;
private JSONArray JArr = null;
and then after;
URL url = null;
try {
url = new URL("your url is here");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JSONParser jParser = new JSONParser();
String json_value = jParser.getJSONFromUrl(url);
try {
JArr = new JSONArray(json_value);
} catch (JSONException e) {
e.printStackTrace();
}
jObj = null;
for (int i = 0; i < JArr.length(); i++) {
try {
jObj = JArr.getJSONObject(i);
topic_id = jObj.getInt("value");
} catch (JSONException e) {
e.printStackTrace();
}
}

Related

org.apache.harmony.xml.ExpatParser$ParseException: At line 292,column 0: junk after document element

I am new to XML Parsing, I need to take url feeds and parse them, then save them in the local database. If I pass some other Link it accepts but if I pass the link that I need It give this error.
I am also providing my code part and log cat output along with this.
Can anyone tell me what sort of problem I am facing?
Following is the URL which I need to parse.
private static final String POSTS_URL = "http://bgr.in/feed/iphoneapp";
This is the method where the error is generated:-
public static void updateStories(Context context, String url, int type) {
if (StoryService.isConnected(context)) {
HttpGet request = new HttpGet(url);
request.addHeader("accepts", "application/rss+xml");
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
Log.e(TAG, "Failed to get rss feed.", e);
}
RootElement root = new RootElement("rss");
List<BaseStory> stories = Story.appendArrayListener(type,
root.getChild("channel"), 0);
try {
Xml.parse(response.getEntity().getContent(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "Failed to parse " + url, e);
}`public static void updateStories(Context context, String url, int type) {
if (StoryService.isConnected(context)) {
HttpGet request = new HttpGet(url);
request.addHeader("accepts", "application/rss+xml");
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
try {
response = client.execute(request);
} catch (IOException e) {
Log.e(TAG, "Failed to get rss feed.", e);
}
RootElement root = new RootElement("rss");
List<BaseStory> stories = Story.appendArrayListener(type,
root.getChild("channel"), 0);
try {
Xml.parse(response.getEntity().getContent(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "Failed to parse " + url, e);
}
The Log Cat Output is as follows:-
System.err(19251): org.apache.harmony.xml.ExpatParser$ParseException: At line 292,column 0: junk after document element
System.err(19251): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:515)
System.err(19251): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
System.err(19251): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:316)
System.err(19251): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
System.err(19251): at android.util.Xml.parse(Xml.java:84)
System.err(19251): at in.bgr.service.StoryService.updateStories(StoryService.java:122)
System.err(19251): at in.bgr.service.StoryService.updateStories(StoryService.java:96)
System.err(19251): at in.bgr.ui.AllStoriesFragment$LoadStoriesTask.doInBackground(AllStoriesFragment.java:309)
System.err(19251): at in.bgr.ui.AllStoriesFragment$LoadStoriesTask.doInBackground(AllStoriesFragment.java:1)
System.err(19251): at android.os.AsyncTask$2.call(AsyncTask.java:288)
System.err(19251): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
System.err(19251): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
System.err(19251): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
"junk after document element" error is caused where there is any unwanted tag in the xml after root element tag. just removes all those tags which are there after the root element tag.
In my case there was a repeated entry of XML element at the line number 292 in the XML file. I cleared it and now I think it will work for me.

Downloading a JSON Array from a URL in Android

I'm trying to download a JSON file in this format
[
{
"CRN":"10001",
"Course":"REG1"
},
{
"CRN":"10002",
"Course":"REG2"
}
]
I understand how to use a JSONArray class once it is created but I don't know how to create the JSONArray object from the file. If the URL location of the file were to be "www.test.com" how would I go about downloading it in background upon the launch of my application so as to not interfere with the launching of the app but not require the user to manually download it themselves.
You might want to check out this helpful library: Retrofit
It makes grabbing and parsing JSON data easy!
I think you should look for Android Web Service example. Where you can find info about
1.How to make a HTTP request to server (using URL eg. www.google.com)
2. How to handle Response from Server
3. How to parse JSON/XML response from Server etc.
Here is the Simple Tutorial I Found for you.
Android Web service for Log-in and Registration
Just go through step by step.
In the example we are making request to server for login and getting response then going ahead in app.
Here is the code snipp.
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
A good way to download the JSON file automatically, would be to launch an AsyncTask during your onCreate method of the home activity.
JSON files are nothing more than text files in a special format, so the could be easily downloaded as a response from a HttpURLConnection, and then be treated as a String.
A suggestion for parsing the JSON objets into Java objects would be the Jackson JSON Processor. You could use the class ObjectMapper of this library to automatically create the objects.
If you are planing to implement the server side by yourself, and you also need a library to send JSON objects, you could use Jersey on both server and client.

android HttpGet incomplete response BufferedReader

Im doing a simple http get,
I see on my result an incomplete response,
what Im doing wrong?
here the code:
class GetDocuments extends AsyncTask<URL, Void, Void> {
#Override
protected Void doInBackground(URL... urls) {
Log.d("mensa", "bajando");
//place proper url
connect(urls);
return null;
}
public static void connect(URL[] urls)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet("http://tiks.document.dev.chocolatecoded.com.au/documents/api/get?type=tree");
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
Log.d("mensa",response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
// now you have the string representation of the HTML request
Log.d("mensa", "estratagema :: "+result);
JSONObject jObject = new JSONObject(result);
Log.d("mensa", "resposta jObject::"+jObject);
Log.d("mensa", "alive 1");
JSONArray contacts = null;
contacts = jObject.getJSONArray("success");
Log.d("mensa", "resposta jObject::"+contacts);
Log.d("mensa", "alive");
//instream.close();
}
} catch (Exception e) {}
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
Log.d("mensa", "linea ::"+line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
i call it with:
GetDocuments get = new GetDocuments();
URL url = null;
try {
url = new URL("ftp://mirror.csclub.uwaterloo.ca/index.html");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//URL url = new URL("http://www.google.es");
get.execute(url);
edit 1
I refer to incomplete as the response that gets truncated?
please notice in below image of response how string gets truncated,
is this because of the log size?,
but the other problem is that it doesn't parse?
thanks!
I don't know if this is going to resolve your problem but you can get rid of your method and use simply:
String responseString = EntityUtils.toString(response.getEntity());
I've had exactly the same issue for the last couple of days. I found that my code worked over WiFi but not 3G. In other words I eliminated all the usual threading candidates. I also found that when I ran the code in the debugger and just waited for (say) 10 seconds after client.execute(...) it worked.
My guess is that
response = httpclient.execute(httpget);
is an asynchronous call in itself and when it's slow returns a partial result... hence JSON deserialization goes wrong.
Instead I tried this version of execute with a callback...
try {
BasicResponseHandler responseHandler = new BasicResponseHandler();
String json = httpclient.execute(httpget, responseHandler);
} finally {
httpclient.close();
}
And suddenly it all works. If you don't want a string, or want your own code then have a look at the ResponseHandler interface. Hope that helps.
I have confirmed that this is because size limit of java string. I have checked this by adding the string "abcd" with the ressponse and printed the response string in logcat. But the result is the truncated respose without added string "abcd".
That is
try {
BasicResponseHandler responseHandler = new BasicResponseHandler();
String json = httpclient.execute(httpget, responseHandler);
json= json+"abcd";
Log.d("Json ResponseString", json);
} finally {
httpclient.close();
}
So I put an arrayString to collect the response. To make array, I splitted My json format response by using "}"
The code is given below(This is a work around only)
BasicResponseHandler responseHandler = new BasicResponseHandler();
String[] array=client.execute(request, responseHandler).split("}");
Then you can parse each objects in to a json object and json array with your custom classes.
If you get any other good method to store response, pls share because i am creating custom method for every different json responses );.
Thank you
Arshad
Hi Now I am using Gson library to handle the responses.
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
Thanks
Arshad
I cant' comment directly due to reputation, but in response to https://stackoverflow.com/a/23247290/4830567 I felt I should point out that the size limit of a Java String is about 2GB (Integer.MAX_VALUE) so this wasn't the cause of the truncation here.
According to https://groups.google.com/d/msg/android-developers/g4YkmrFST6A/z8K3vSdgwEkJ it is logcat that has a size limit, which is why appending "abcd" and printing in logcat didn't work. The String itself would have had the appended characters. The previously linked discussion also mentioned that size limits with the HTTP protocol itself can occasionally be a factor, but that most servers and clients handle this constraint internally so as to not expose it to the user.

android - error while parsing JSON response from server

I'll try be to brief, please ask if something is unclear. I'm getting a user's audio list from vk.com (a large social network in case someone doesn't know). The response looks like:
{"response":[{
"aid":"60830458","owner_id":"6492","artist":"Noname","title":"Bosco",
"duration":"195","url":"http:\/\/cs40.vkontakte.ru\/u06492\/audio\/2ce49d2b88.mp3"},
{"aid":"59317035","owner_id":"6492","artist":"Mestre Barrao","title":"Sinhazinha",
"duration":"234","url":"http:\/\/cs510.vkontakte.ru\/u2082836\/audio\/
d100f76cb84e.mp3"}]}
Usually it is much longer since a user can have hundreds or even thousands of tracks on his profile. Artist and title can also contain cyrillic letters, that's why I used UTF-8 in the Parser. I'm not really familiar with JSON, I'm trying to parse the response using the following:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public static JSONObject getJSONFromUrl(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
But the app crashes with an IllegalArgumentException exception (Illegal character in scheme at index 0):
02-27 10:37:35.870: E/AndroidRuntime(21038): FATAL EXCEPTION: main
02-27 10:37:35.870: E/AndroidRuntime(21038): java.lang.RuntimeException: Unable to resume activity {com.vadim.android.vk_player/com.vadim.android.vk_player.MainActivity}: java.lang.IllegalArgumentException: Illegal character in scheme at index 0: {"response":[{"aid":191819427,"owner_id":13590837,"artist":"Buena Vista Social Club","title":"El Cuarto de Tula","duration":445,"url":"http:\/\/cs548.userapi.com\/u361189\/audios\/b8c6a3bdb0bb.mp3","lyrics_id":"1133390"},{"aid":191477921,"owner_id":13590837,"artist":"Buena Vista Social Club","title":"Hasta Siempre Comandante Che Guevara","duration":193,"url":"http:\/\/cs4515.userapi.com\/u7198823\/audios\/5fafa2136e16.mp3","lyrics_id":"2876258"},{"aid":190900891,"owner_id":13590837,"artist":"Slade","title":"Oh la la in L.A.","duration":229,"url":"http:\/\/cs4962.userapi.com\/u9811745\/audios\/ed7445d38bef.mp3"},{"aid":188976833,"owner_id":13590837,"artist":"PR-MEX","title":"У Билли Гейтса","duration":126,"url":"http:\/\/cs5002.userapi.com\/u4693819\/audios\/a1899ebb7716.mp3","lyrics_id":"5201762"},{"aid":186998450,"owner_id":13590837,"artist":"The Best Latino Dance","title":"2Sweet-Bomba Latina","duration":213,"url":"http:\/\/cs4341.userapi.com\/u49441496\/audios\/788cd8243842.mp3"},{"aid":186486990,"owner_id":13590837,"artist":"001 Track No05 Latin music 9","title":"001 Track No05 Latin music 9","duration":226,"url":"http:\/\/cs4341.userapi.com\/u25293142\/audios\/277e46d451d4.mp3"},{"aid":185813300,"owner_id":13590837,"artist":"Латино ?? ","title":" Самбо","duration":190,"url":"http:\/\/cs4206.userapi.com\/u2183525\/audios\/678fe97a8700.mp3","lyrics_id":"4944025"},{"aid":185805191,"owner_id":13590837,"artist":"Дженифер Лопес","title":"Латино","duration":212,"url":"http:\/\/cs4220.userapi.com\/u33799853\/audios\/685f4bc7024d.mp3","lyrics_id":"3985793"},{"aid":185355131,"owner_id":13590837,"artist":"Latino","title":"Afa-Na-Na","duration":174,"url":"http:\/\/cs548.userapi.com\/u406078\/audios\/5e771c6958c4.mp3","lyrics_id":"8840070"},{"aid":185167860,"owner_id":13590837,"artist":"Batuka-Latino_StepMIX(137bpm)","title":"demo","duration":232,"url":"http:\/\/cs4863.userapi.com\/u43189860\/audios\/b6a08490146a.mp3","lyrics_id":"10200160"},{"aid":185143167,"owner_id":13590837,"artist":"Pr, Mex","title":"Ставил Windows программист","duration":130,"url":"http:\/\/cs4246.userapi.com\/u3476823\/audios\/75161ed38448.mp3","lyrics_id":"2012814"},{"aid":185141056,"owner_id":13590837,"artist":"Antony Melnyk, Sergiy Tykhanskyy ","title":"Debugging Song","duration":234,"url":"http:\/\/cs6126.userapi.com\/u42350435\/audios\/f83f20d8d754.mp3","lyrics_id":"36053942"},{"aid":185141033,"owner_id":13590837,"artist":"админ","title":"чистый дос","duration":173,"url":"http:\/\/cs4429.userapi.com\/u9853602\/audios\/2b77464f9193.mp3"},{"aid":184547392,"owner_id":13590837,"artist":"Geri Halliwell","title":"Mi chico latino (samba)","duration":194,"url":"http:\/\/cs5057.userapi.com\/u8186180\/audios\/67119f2af914.mp3"},{"aid":184022338,"owner_id":13590837,"artist":"Elena Paparizou","title":"My number one","duration":176,"url":"http:\/\/cs1092.userapi.com\/u830723\/audios\/25552d1f7e40.mp3","lyrics_id":"6640643"},{"aid":183519519,"owner_id":13590837,"artist":"Latino - Samba - Elena Paparizou","title":"Gigolo","duration":203,"url":"http:\/\/cs4405.userapi.com\/u3609345\/audios\/5255ecdda950.mp3","lyrics_id":"7216473"},{"aid":183219402,"owner_id":13590837,"artist":"David Bisbal ","title":" Llorare las penas (самба)","duration":260,"url":"http:\/\/cs5003.userapi.com\/u32245826\/audios\/fe718c40aed1.mp3"},{"aid":183110662,"owner_id":13590837,"artist":"Juanes","title":"La soledad","duration":193,"url":"http:\/\/cs4615.userapi.com\/u400878\/audios\/40abd9dcb4f5.mp3","lyrics_id":"7753114"},{"aid":180455728,"owner_id":13590837,"artist":"Guns N' Roses","title":"Sweet Child O' Mine","duration":356,"url":"http:\/\/cs5125.userapi.com\/u1412326\/audios\/1fc190388445.mp3","lyrics_id":"5582681"},{"aid":180317426,"owner_id":1359083
Any ideas what I'm doing wrong and what would be the correct way to parse the response of given format? There are a lot of apps using the same API so the JSON is correct.. No clue what's wrong here
Illegal Argument Exception comes in JSON case while reading it. So invalid json because of those urls. Also for next communication, place the logcat as the text pls.
Try to display the JSON response in JSONlint.com without this url part of :
"url": "http:\/\/cs510.vkontakte.ru\/u2082836\/audio\/
d100f76cb84e.mp3"
You will find, the response will be validated properly.
I think there is some space characters present in the url part (between audio\/ and d100f76cb84e.mp3) which is coming from response:
"url": "http:\/\/cs510.vkontakte.ru\/u2082836\/audio\/
d100f76cb84e.mp3"

Getting "FATAL EXCEPTION : AsyncTask #2". And I don't know what's causing it

While trying to call a web service and get the corresponding json object I get a fatal exception. I have absolutely no idea where to look and what errors to correct.
EDIT:
private class CallServiceTask extends AsyncTask<Object, Void, JSONObject>
{
protected JSONObject doInBackground(Object... params)
{
HttpGet req = (HttpGet) params[0];
String url = (String) params[1];
return executeRequest(req, url);
}
}
And here's executeRequest method called in doInBackground:
private JSONObject executeRequest(HttpGet request, String url)
{
HttpClient client = new DefaultHttpClient();
JSONObject jsonObj = null;
client = getNewHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String response = convertStreamToString(instream);
try {
jsonObj = new JSONObject(response);
} catch (JSONException e1) {
e1.printStackTrace();
}
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
}
return jsonObj;
}
Just looking at your LogCat stack trace (in this case) it tells you all you need to know about what this exception is and what has caused it:
thread exiting with uncaught exception
Tells you that an exception has been thrown which your code does not handle
An error occurred while executing doInBackground()
This tells you that your doInBackground() function in your Async task has thrown this unhandled exception
Caused by: java.lang.ClassCastException ...HttpPost... (RestClient.java:275)
And that tells you that you have encountered a ClassCastException, resulting from a HttpPost call at line 275 in that source file.
EDIT:
Should have read that stack trace more carefully... as HandlerExploit has posted It's the HttpPost that's throwing that error, where you're expecting a HttpGet... but the following debug method still stands:
If you add an extra catch (ClassCastException e) with an e.getMessage() you'll most likely see a useful error message that describes the problem in more detail.
When in this situation and I find an unexpected exception being thrown like this I tend to add a temporary 'catch all' (catch (Exception e) { e.printStackTrace() } ) and stick a break point on the e.printStackTrace() so I can see all the details about the exception... might not be the most efficient way of doing it but its a start when you're in the dark!
My best guess would be that :
HttpGet req = (HttpGet) params[0];
Is returning a HttpPost instead of a HttpGet.
Please post where you are calling new CallServiceTask().execute();

Categories

Resources