IM遇到以下代码问题我使用Parse JSON对象和数组。请参阅以下JSON数据我试图从文本中获取文本并插入TextViews。出于某种原因,目前我遇到了summary
无法找到的错误。有关详细信息,请参阅JSONParser类。
尝试解析以下URL是针对摘要数据的。
https://api.company.com/api/systems/165756/summary?&key=e1e63de7276b04c9bb99adfd45b3a14c
在JSON验证器中检查URL(http://jsonlint.com/)我得到以下
{
"energy_month": 31132,
"current_power": 4210,
"modules": 24,
"energy_today": 14666,
"system_id": 165756,
"energy_week": 215504,
"source": "microinverters",
"energy_lifetime": 1545467,
"summary_date": "2013-05-02T00:00:00-07:00"
}
这里的代码是如何使用来尝试解析其中的一些字符串(energy_month)(current_power)等。在我的JSONParser.java中,我必须要类来解析数组或对象。这可能需要修复。
protected ArrayList<String> doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
arrfortextviews=new ArrayList<String>();
// Using APIKEY from strings.xml
String apikey = getString(R.string.apikey);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String systemID = settings.getString("systemID", "");
//JSONArray json = jParser.getJSONFromUrl(url2 + systemID + "/summary?&key=" + apikey);
JSONObject json2 = jParser.getJSONFromUrl2(url2 + systemID + "/summary?&key=" + apikey);
//for (int i = 0; i < json2.length(); i++) {
//i = index
try {
//JSONObject c = json2.getJSONObject(i);
//JSONObject geometry = json2.getJSONObject("summary");
Log.e("JSON Parser", json2 + args.toString());
String current_power = json2.getString(TAG_CURRENT_POWER);
String energy_lifetime = json2.getString(TAG_ENERGY_LIFETIME);
arrfortextviews.add(current_power);
arrfortextviews.add(energy_lifetime);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("JSON Parser", json2 + e.toString());
}
return arrfortextviews;
}
JSONParser.class
public class JSONParser {
private static final Context context = null;
static InputStream is = null;
static JSONObject jarray = null;
static JSONArray jarray2 = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl2(String url) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e("==>", "No Response, Check Your API KEY");
Toast.makeText(context,"Error Response, Check your API Key", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
JSONObject jobj = new JSONObject(builder.toString());
jarray = jobj.getJSONObject("summary");
Log.e("JSON Parser", jobj + url.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
Log.e("JSON Parser", json + url + e.toString());
}
// return JSON String
return jarray;
}
public JSONArray getJSONFromUrl(String url) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e("==>", "No Response, Check Your API KEY");
Toast.makeText(context,"Error Response, Check your API Key", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
JSONObject jobj = new JSONObject(builder.toString());
jarray2 = jobj.getJSONArray("systems");
Log.e("JSON Parser", jarray2 + json + url.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
Log.e("JSON Parser", json + url + e.toString());
}
// return JSON String
return jarray2;
}