I am developing an application where the process to get data from server is
Step 1: Post a request using Http POST to url with specific header content.
Step 2: In return, i will get part of a link, which i have to construct into a full URL
Strp 3: Now, pass this URL using Http GET method to obtain the data.
Now i am trying to achieve this using following code,
@Override
protected String doInBackground(String... params)
{
try
{
/** Performing POST method to url to obtain link for data**/
HttpClient httpClient = HttpClientConstant.getThreadSafeClient();
HttpPost httpPost = new HttpPost(CHANNEL_LANGUAGE_SENTIMENT_URL);
httpPost.setEntity(new ByteArrayEntity(POST_URL_VALUES.toString().getBytes("UTF8")));
HttpResponse response = httpClient.execute(httpPost);
if(response!=null)
{
OBTAIN_URL_LINK = convertJsonToString(response.getEntity().getContent()).toString();
/** getting the link in OBTAIN_URL_LINK**/
System.out.println("Response: " + OBTAIN_URL_LINK);
try
{
/** Constructing the URL**/
JSONObject mainStringJSON = new JSONObject(OBTAIN_URL_LINK);
REFINED_URL_LINK = mainStringJSON.getString("poll_url");
REQUIRED_URL_LINK = PREDEFINED_URL_LINK + REFINED_URL_LINK;
System.out.println("URL to USE: " + REQUIRED_URL_LINK);
/** Performing GET method to obtain the Data**/
HttpClient channelHttpClient = HttpClientConstant.getThreadSafeClient();
HttpGet channelHttpGet = new HttpGet(REQUIRED_URL_LINK);
HttpResponse channelHttpResponse = channelHttpClient.execute(channelHttpGet);
/** Reading the JSON response and stroing in string**/
BufferedReader channelHttpReader = new BufferedReader(new InputStreamReader(channelHttpResponse.getEntity().getContent(), "UTF-8") );
CHANNEL_LANGUAGE_RESPONSE_JSON = channelHttpReader.readLine();
System.out.println(CHANNEL_LANGUAGE_RESPONSE_JSON);
if(CHANNEL_LANGUAGE_RESPONSE_JSON != null)
{
try
{
JSONObject mainJson = new JSONObject(CHANNEL_LANGUAGE_RESPONSE_JSON);
JSONObject dataJson = mainJson.getJSONObject("data");
//JSONObject dataJson = new JSONObject().getJSONObject("data");
/** Values for Channel Graphs**/
JSONObject channelJson = dataJson.getJSONObject("category");
int twitterValues = channelJson.getInt("twitter");
When i debugg the application, it working fine and it shows the data. But when i run the application it's crashing and giving me this,
10-07 09:03:10.517: I/System.out(1330): org.json.JSONException: No value for data
10-07 09:03:10.517: I/System.out(1330): at org.json.JSONObject.get(JSONObject.java:354)
10-07 09:03:10.517: I/System.out(1330): at org.json.JSONObject.getJSONObject(JSONObject.java:569)
10-07 09:03:10.557: I/System.out(1330): at m_brain.m_adaptive.m_DetailActivity.ChannelsAndLanguageFilterFragment$GetChannelAndLanguageData.doInBackground(ChannelsAndLanguageFilterFragment.java:164)
10-07 09:03:10.597: I/System.out(1330): at m_brain.m_adaptive.m_DetailActivity.ChannelsAndLanguageFilterFragment$GetChannelAndLanguageData.doInBackground(ChannelsAndLanguageFilterFragment.java:1)
10-07 09:03:10.597: I/System.out(1330): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-07 09:03:10.637: I/System.out(1330): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-07 09:03:10.637: I/System.out(1330): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-07 09:03:10.637: I/System.out(1330): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-07 09:03:10.669: I/System.out(1330): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-07 09:03:10.708: I/System.out(1330): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-07 09:03:10.747: I/System.out(1330): at java.lang.Thread.run(Thread.java:856)
10-07 09:03:10.917: I/Choreographer(1330): Skipped 35 frames! The application may be doing too much work on its main thread.
10-07 09:03:11.047: D/AndroidRuntime(1330): Shutting down VM
10-07 09:03:11.047: W/dalvikvm(1330): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-07 09:03:11.077: E/AndroidRuntime(1330): FATAL EXCEPTION: main
10-07 09:03:11.077: E/AndroidRuntime(1330): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-07 09:03:11.077: E/AndroidRuntime(1330): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-07 09:03:11.077: E/AndroidRuntime(1330): at java.util.ArrayList.get(ArrayList.java:304)
10-07 09:03:11.077: E/AndroidRuntime(1330): at m_brain.m_adaptive.m_DetailActivity.ChannelsAndLanguageFilterFragment.generatePieDataChannels(ChannelsAndLanguageFilterFragment.java:311)
10-07 09:03:11.077: E/AndroidRuntime(1330): at m_brain.m_adaptive.m_DetailActivity.ChannelsAndLanguageFilterFragment.access$11(ChannelsAndLanguageFilterFragment.java:304)
10-07 09:03:11.077: E/AndroidRuntime(1330): at m_brain.m_adaptive.m_DetailActivity.ChannelsAndLanguageFilterFragment$GetChannelAndLanguageData.onPostExecute(ChannelsAndLanguageFilterFragment.java:270)
10-07 09:03:11.077: E/AndroidRuntime(1330): at m_brain.m_adaptive.m_DetailActivity.ChannelsAndLanguageFilterFragment$GetChannelAndLanguageData.onPostExecute(ChannelsAndLanguageFilterFragment.java:1)
10-07 09:03:11.077: E/AndroidRuntime(1330): at android.os.AsyncTask.finish(AsyncTask.java:631)
10-07 09:03:11.077: E/AndroidRuntime(1330): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-07 09:03:11.077: E/AndroidRuntime(1330): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-07 09:03:11.077: E/AndroidRuntime(1330): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 09:03:11.077: E/AndroidRuntime(1330): at android.os.Looper.loop(Looper.java:137)
10-07 09:03:11.077: E/AndroidRuntime(1330): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-07 09:03:11.077: E/AndroidRuntime(1330): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 09:03:11.077: E/AndroidRuntime(1330): at java.lang.reflect.Method.invoke(Method.java:511)
10-07 09:03:11.077: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-07 09:03:11.077: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-07 09:03:11.077: E/AndroidRuntime(1330): at dalvik.system.NativeStart.main(Native Method)
I am doing this in a Fragment and displaying Data in Graphs which are in a ListView. I am beginner in this, so please guide me if i am doing it in a wrong way or need a different approach. Moreover i am not able to understand if this is hapening because of the Http request or anything else. The line it shows point at end of the try method.