I need some advice on manipulating JSON data. I've come to an absolute conclusion that there is something wrong with the JSON data that I am using for my blog.
I'm feeding the JSON data into an android application to display information on the app. The problem I am having is that when I use my JSON data I get a negativearraysize exception.
Now before you blame the code, know that I got the app to work fine using the teamtreehouse blog JSON code to work here:
http://blog.teamtreehouse.com/api/get_recent_summary/
SO this works fine. I made my JSON code look almost exactly like this code. The arrays a nested the same, same naming conventions are used... etc..
Here is my JSONcode that brings back the exception when its plugged in to my android code.
http://www.evotechmachine.com/api/get_recent_posts/?include=title,url,status,id,date
If you compare the two JSON codes you'll see that they are pretty much the same on the inside. The only thing I reference in either is the Posts, title, and url. I'm sure its not a mismatch of names. I'm at a total loss. thanks in advance people.
public static final int NUMBER_OF_POSTS = 20;
public static final String TAG = MainListActivity.class.getSimpleName();
protected JSONObject mBlogData;
protected ProgressBar mProgressBar;
private final String KEY_TITLE = "title";
private final String KEY_AUTHOR = "author";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
if (isNetworkAvailable()) {
mProgressBar.setVisibility(View.VISIBLE);
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
}
else {Toast.makeText(this, "network is unavailable", Toast.LENGTH_LONG).show();
}
// Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
JSONArray jsonPosts;
try {
jsonPosts = mBlogData.getJSONArray("posts");
JSONObject jsonPost = jsonPosts.getJSONObject(position);
String blogUrl = jsonPost.getString("url");
Intent intent = new Intent(this, BlogWebViewActivity.class);
intent.setData(Uri.parse(blogUrl));
startActivity(intent);
} catch (JSONException e) {
logException(e);
}
}
private void logException(Exception e) {
Log.e(TAG, "Exception caught!", e);
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void handleBlogResponse() {
mProgressBar.setVisibility(View.INVISIBLE);
if(mBlogData==null) {
updateDisplayForError();
}
else {
try {
JSONArray jsonPosts = mBlogData.getJSONArray("posts");
ArrayList<HashMap<String, String>> blogPosts =
new ArrayList<HashMap<String, String>>();
for (int i = 0;i< jsonPosts.length();i++) {
JSONObject post = jsonPosts.getJSONObject(i);
String title = post.getString(KEY_TITLE);
title = Html.fromHtml(title).toString();
String author = post.getString(KEY_AUTHOR);
author = Html.fromHtml(author).toString();
HashMap<String, String> blogPost = new HashMap<String, String>();
blogPost.put(KEY_TITLE, title);
blogPost.put(KEY_AUTHOR, author);
blogPosts.add(blogPost);
}
String[] keys= { KEY_TITLE, KEY_AUTHOR };
int[] ids = {android.R.id.text1, android.R.id.text2};
SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, android.R.layout.simple_list_item_2, keys, ids);
setListAdapter(adapter);
} catch (JSONException e) {
logException(e);
}
}
}
private void updateDisplayForError() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.error_title));
builder.setMessage(getString(R.string.error_message));
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
TextView emptyTextView = (TextView) getListView().getEmptyView();
emptyTextView.setText(getString(R.string.no_items));
}
private class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Object... arg0) {
int responseCode = -1;
JSONObject jsonResponse = null;
try {
URL blogFeedUrl = new URL("http://www.evotechmachine.com/api/get_recent_posts/?include=title,url,status,id,date");
HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int contentLength = connection.getContentLength();
char[] charArray = new char[contentLength];
reader.read(charArray);
String responseData = new String(charArray);
jsonResponse = new JSONObject(responseData);
} else {
Log.i(TAG, "unsuccessful HTTP Response Code: " +responseCode);
}
Log.i(TAG, "Code: " + responseCode);
} catch (MalformedURLException e) {
logException(e);}
catch (IOException e) {
logException(e);
}
catch (Exception e) {
logException(e);
}
return jsonResponse;
}
@Override
protected void onPostExecute(JSONObject result) {
mBlogData = result;
handleBlogResponse();
}
}
}
here's the logcat:
07-31 20:49:08.209: E/(32544): <s3dReadConfigFile:75>: Can't open file for reading
07-31 20:49:08.209: E/(32544): <s3dReadConfigFile:75>: Can't open file for reading
07-31 20:49:09.280: E/MainListActivity(32544): Exception caught!
07-31 20:49:09.280: E/MainListActivity(32544): java.lang.NegativeArraySizeException: -1
07-31 20:49:09.280: E/MainListActivity(32544): at com.nibbdigital.blogreader.MainListActivity$GetBlogPostsTask.doInBackground(MainListActivity.java:168)
07-31 20:49:09.280: E/MainListActivity(32544): at com.nibbdigital.blogreader.MainListActivity$GetBlogPostsTask.doInBackground(MainListActivity.java:1)
07-31 20:49:09.280: E/MainListActivity(32544): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-31 20:49:09.280: E/MainListActivity(32544): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-31 20:49:09.280: E/MainListActivity(32544): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-31 20:49:09.280: E/MainListActivity(32544): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-31 20:49:09.280: E/MainListActivity(32544): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-31 20:49:09.280: E/MainListActivity(32544): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-31 20:49:09.280: E/MainListActivity(32544): at java.lang.Thread.run(Thread.java:856)
Your content-length is negative (ie unknown)
Check out this for some insights
https://stackoverflow.com/a/5428670/770467
Hope it helps
int contentLength = connection.getContentLength(); returns negative length
This is because your server does not set the content-length header in the http response. So two solutions: 1. You can fix the server side code 2. You can process the input response this way to convert to a string:
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(inputStream);
StringBuilder sb = new StringBuilder();
for (String line; (line = br.readLine()) != null;) {
sb.append(line);
sb.append("\n");
}
return sb.toString();
}
finally
{
if (reader != null) {
reader.close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.