Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:21:19+00:00 2026-05-24T04:21:19+00:00

Here’s the code for my JSON parsing app. It force closes as soon as

  • 0

Here’s the code for my JSON parsing app. It force closes as soon as I run it on my emulator. I am new to Android programming, and I’m self taught so please bear with me. Thanks!

JSONfunctions.java:

package com.pxr.tutorial.json;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONfunctions {

public static JSONObject getJSONfromURL(String url){
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }

  //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new            
                InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }

    try{

        jArray = new JSONObject(result);            
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return jArray;
}
}

Main.java:

package com.pxr.tutorial.json;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.pxr.tutorial.xmltest.R;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, 
    String>> ();


    JSONObject json = JSONfunctions.getJSONfromURL
("http://www.tastekid.com/ask/ws?q=mosdef&f=musifin2125&k=mjjlnzkyzwuz&format=JSON");                
    try{

        JSONArray  similar = json.getJSONArray("similar");

        for(int i=0;i<similar.length();i++){                        
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = similar.getJSONObject(i);

            map.put("id",  String.valueOf(i));
            map.put("name", "Name:" + e.getString("name"));
            map.put("type", "Type: " +  e.getString("type"));
            mylist.add(map);            
        }       
    }catch(JSONException e)        {
         Log.e("log_tag", "Error parsing data "+e.toString());
    }

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                    new String[] { "name", "type" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {                  
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>)
lv.getItemAtPosition(position);                 
            Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.",   
Toast.LENGTH_SHORT).show(); 
        }
    });
}
}

EDIT – Fixed unhandled exception, but now it says “No data” when I try to parse the URL.

Thanks again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:21:20+00:00Added an answer on May 24, 2026 at 4:21 am

    Here are your mistakes:

    JSONArray similar = json.getJSONArray("similar");

    1. In parsing JSON you have to note that it’s case sensitive, i see that your json object is Similiar but you try to get it as similiar.
    2. Similiar is a JSON Object but you try to get it as a JSONArray

    Here is the fix for your codes, i give you a fix to get Info inside Similiar object:

            JSONObject earthquakes = json.getJSONObject("Similar");
            JSONArray info = earthquakes.getJSONArray("Info");
    
            for (int i = 0; i < info.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject e = info.getJSONObject(i);
    
                map.put("id", String.valueOf(i));
                map.put("name", "Name:" + e.getString("Name"));
                map.put("type", "Type: " + e.getString("Type"));
                mylist.add(map);
            }
    

    Replace your code block inside try after JSONObject json = JSONFun.getJSONfromURL("http://www.tastekid.com/ask/ws? q=mosdef&f=musifin2125&k=mjjlnzkyzwuz&format=JSON");
    with code above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's some of my production code (I had to force line breaks): task =
Here is my code, which takes two version identifiers in the form 1, 5,
Here is the scenario: I'm writing an app that will watch for any changes
Here is the code I'm using http://jsbin.com/evike5/edit When the jQuery UI dialog is fired
Here is a great article about GC may occur at unexpected point of code
Here is my javascript code for a cursor focus function to go to username
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?
Here is my code (Say we have a single button on the page that
here are 2 screen shots when i try to debug my code in visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.