【发布时间】:2015-04-10 20:57:27
【问题描述】:
我正在尝试在 Android Studio 中解析此 JSON 链接,但出现错误 E/ServiceHandler﹕无法从 url 取得任何数据。 https://api.jivedata.com/financials/detail/?ticker=aapl&elements=price_to_earnings
这是我的 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);
}
}
}
这里是 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;
}
}
【问题讨论】:
-
嗯,
public class MainActivity在哪里? -
请发布您的
list_item.xml。 -
Android 清单中有
<uses-permission android:name="android.permission.INTERNET" />吗?