I am trying to parse this JSON link in Android Studio but I get the error E/ServiceHandler﹕ Couldn't get any data from the url. https://api.jivedata.com/financials/detail/?ticker=aapl&elements=price_to_earnings
Here is my MainActivity.java
package claudio.jivedatapeparsing;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
private static String url = "https://api.jivedata.com/financials/detail/?ticker=aapl&elements=price_to_earnings";
// JSON Node names
private static final String TAG_VALUE = "value";
private static final String TAG_TOTAL = "_total_";
private static final String TAG_RESULTS = "_results_";
private static final String TAG_PRICETOEARNINGS = "price_to_earnings";
JSONArray _total_ = null;
// Hashmap for ListView
// Hashmap is an implementation of map
ArrayList<HashMap<String, Integer>> contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<HashMap<String, Integer>>();
ListView lv = getListView();
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
//before it executes
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObject = new JSONObject(jsonStr);
JSONObject _results_ = jsonObject.getJSONObject(TAG_RESULTS);
JSONObject price_to_earnings = _results_.getJSONObject(TAG_PRICETOEARNINGS);
JSONArray _total_ = price_to_earnings.getJSONArray(TAG_TOTAL);
for (int i = 0; i < _total_.length(); i++) {
JSONObject c = _total_.getJSONObject(31);//get the latest P/E value
int value = c.getInt(TAG_VALUE);
HashMap<String, Integer> contact = new HashMap<String, Integer>();
contact.put(TAG_VALUE, value);
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[] { TAG_VALUE }, new int[] { R.id.value });
setListAdapter(adapter);
}
}
}
and here is the ServiceHandler.java:
package claudio.jivedatapeparsing;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
Edit: I was able to modify the ServiceHandler.java class to use HTTPUrlConnection instead of the old APIs, and it worked.
Modified ServiceHandler.java:
import org.apache.http.NameValuePair;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
HttpURLConnection urlConnection;
public ServiceHandler() {
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
StringBuilder result = new StringBuilder();
try {
URL urlObj = new URL(url);
urlConnection = (HttpURLConnection) urlObj.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
}catch( Exception e) {
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
return result.toString();
}
}
Log of response in MainActivity.java:
04-10 17:00:00.351 22478-22517/com.networkjsontest.danu.networkjsontest D/Response:﹕ > {"_results_": {"_dates_": [{"end": "2009-09-26", "length": 12, "start": "2008-09-28"}, {"end": "2009-12-26", "length": 3, "start": "2009-09-27"}, {"end": "2010-03-27", "length": 3, "start": "2009-12-27"}, {"end": "2010-06-26", "length": 3, "start": "2010-03-28"}, {"end": "2010-06-26", "length": 9, "start": "2009-09-27"}, {"end": "2010-09-25", "length": 3, "start": "2010-06-27"}, {"end": "2010-09-25", "length": 12, "start": "2009-09-27"}, {"end": "2010-12-25", "length": 3, "start": "2010-09-26"}, {"end": "2011-03-26", "length": 3, "start": "2010-12-26"}, {"end": "2011-03-26", "length": 6, "start": "2010-09-26"}, {"end": "2011-06-25", "length": 3, "start": "2011-03-27"}, {"end": "2011-06-25", "length": 9, "start": "2010-09-26"}, {"end": "2011-09-24", "length": 3, "start": "2011-06-26"}, {"end": "2011-09-24", "length": 12, "start": "2010-09-26"}, {"end": "2011-12-31", "length": 3, "start": "2011-09-25"}, {"end": "2012-03-31", "length": 3, "start": "2012-01-01"}, {"end": "2012-03-31", "length": 6, "start": "2011-09-25"}, {"end": "2012-06-30", "length": 3, "start": "2012-04-01"}, {"end": "2012-06-30", "length": 9, "start": "2011-09-25"}, {"end": "2012-09-29", "length": 3, "start": "2012-07-01"}, {"end": "2012-09-29", "length": 12, "start": "2011-09-25"}, {"end": "2012-12-29", "length": 3, "start": "2012-09-30"}, {"end": "2013-03-30", "length": 3, "start": "2012-12-30"}, {"end": "2013-03-30", "length": 6, "start": "2012-09-30"}, {"end": "2013-06-29", "length": 3, "start": "2013-03-31"}, {"end": "2013-06-29", "length": 9, "start": "2012-09-30"}, {"end": "2013-09-28", "length": 3, "start": "2013-06-30"}, {"end": "2013-09-28", "length": 12, "start": "2012-09-30"}, {"end": "2013-12-28", "length": 3, "start": "2013-09-29"}, {"end": "2014-03-29", "length": 3, "start": "2013-12-29"}, {"end": "2014-03-29", "length": 6, "start": "2013-09-29"}, {"calculated": "LTM", "end": "2014-03-29", "length": 12, "start": "2013-03-31"}], "_labels_": {"price_to_earnings": "Price/Earnings"}, "price_to_earnings": {"_total_": [{"formatted": "86.5x", "value": 86.46023072252581}, {"formatted": "210.8x", "value": 210.7756068679692}, {"formatted": "231.6x", "value": 231.62003903708523}, {"formatted": "218.9x", "value": 218.87488472179527}, {"formatted": "73.4x", "value": 73.36424523441525}, {"formatted": "165.3x", "value": 165.27390900649954}, {"formatted": "50.8x", "value": 50.80996217797759}, {"formatted": "118.6x", "value": 118.58760826115923}, {"formatted": "118.9x", "value": 118.9243360614665}, {"formatted": "59.4x", "value": 59.37786673338337}, {"formatted": "97.4x", "value": 97.42747673782156}, {"formatted": "36.9x", "value": 36.893103269599465}, {"formatted": "107.5x", "value": 107.50415219688962}, {"formatted": "27.5x", "value": 27.467016433917134}, {"formatted": "54.5x", "value": 54.5009185548071}, {"formatted": "61.3x", "value": 61.26312166580623}, {"formatted": "28.8x", "value": 28.842258770153123}, {"formatted": "80.7x", "value": 80.68902991840436}, {"formatted": "21.2x", "value": 21.247388839152492}, {"formatted": "86.6x", "value": 86.58640398881187}, {"formatted": "17.1x", "value": 17.06083914408262}, {"formatted": "54.4x", "value": 54.442575317326806}, {"formatted": "74.6x", "value": 74.57840159212319}, {"formatted": "31.5x", "value": 31.46961325966851}, {"formatted": "103.2x", "value": 103.18840579710145}, {"formatted": "24.1x", "value": 24.115156646909398}, {"formatted": "94.8x", "value": 94.78168264110757}, {"formatted": "19.2x", "value": 19.224019224019223}, {"formatted": "54.5x", "value": 54.467564259485926}, {"formatted": "69.6x", "value": 69.64687469431674}, {"formatted": "30.6x", "value": 30.56449881948916}, {"formatted": "18.9x", "value": 18.882435622033043}]}}}
Note that your JSON parsing code didn't parse it correctly, so you stil have some work to do there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With