0

I am trying to get information using JSON and displaying it on the screen, the url for getting the information is given below:

https://searchcode.com/api/codesearch_I/?q=quicksort&per_page=100

the JSON is given below:

"results": [
{
"repo": "https://github.com/robtsai/mathrocks.git",
"language": "Haskell",
"linescount": 8,
"location": "/algos_datastructures",
"name": "mathrocks",
"url": "https://searchcode.com/codesearch/view/71123349/",
"md5hash": "78fd0c4174ad9af35885b1275db3c805",
"lines": {
"1": "-- quicksort in haskell",
"2": "",
"3": "quicksort :: Ord a => [a] -> [a]",
"4": "quicksort [] = []",
"5": "quicksort (x:xs) = quicksort smallerHalf ++ [x] ++ quicksort largerHalf",
"6": "\twhere"
},
"id": 71123349,
"filename": "quicksort.hs"
},

Here, I am trying to get the name , language and url attribute. The problem is that both name and language attribute returns their respective values but url attribute doesn't, it returns null.

The code is given below :

ResultsList.java

public class ResultsList extends ListActivity {

private ProgressDialog pDialog;

// URL to get contacts JSON
private static String urlmain = "https://searchcode.com/api/codesearch_I/?q=";
private static final String addition="&per_page=100";

// JSON Node names
private static final String ARR_RESULTS="results";
private static final String OBJ_NAME="name";
private static final String OBJ_LANG="language";
private static final String OBJ_URL="url";

// Seach term
private static final String SEARCH_KEYWORD = "seachterm";

// contacts JSONArray
JSONArray contacts = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> resultsList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results_list);

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

    ListView listView=getListView();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.nameofrepo))
                    .getText().toString();
            String lang=((TextView) view.findViewById(R.id.langofrepo))
                    .getText().toString();
            String urlcode = ((TextView) view.findViewById(R.id.urlofcode))
                    .getText().toString();

            Intent intent=new Intent(getApplicationContext(),
                    DisplayResults2.class);
            intent.putExtra(OBJ_NAME, name);
            intent.putExtra(OBJ_LANG,lang);
            intent.putExtra(OBJ_URL, urlcode);
            startActivity(intent);

        }
    });

    Intent intent1=getIntent();

    String search_item=intent1.getStringExtra(SEARCH_KEYWORD);

    // Calling async task to get json
    new GetContacts().execute(search_item);

}


/**
 * Async task class to get json by making HTTP call
 * */
class GetContacts extends AsyncTask<String, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        /*pDialog = new ProgressDialog(ResultsList.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();*/

    }

    @Override
    protected Void doInBackground(String... search_term) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(urlmain+
                URLEncoder.encode(search_term[0])+addition, ServiceHandler.GET);

        Log.d("search term: ", "> " + search_term[0]);
        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(ARR_RESULTS);

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String name = c.getString(OBJ_NAME);
                    String lang = c.getString(OBJ_LANG);
                    String url = c.getString(OBJ_URL);


                    // tmp hashmap for single contact
                    HashMap<String, String> single_result = new HashMap<String, String>();

                    single_result.put(OBJ_NAME, name);
                    single_result.put(OBJ_LANG, lang);
                    single_result.put(OBJ_URL, url);

                    // adding contact to contact list
                    resultsList.add(single_result);
                }
            } 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);
        // Dismiss the progress dialog
        /*if (pDialog.isShowing())
            pDialog.dismiss();*/
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(ResultsList.this,
                resultsList, R.layout.row_item2, new String[] { OBJ_NAME,
                OBJ_LANG,OBJ_URL }, new int[] { R.id.nameofrepo, R.id.langofrepo,
                R.id.url });


        setListAdapter(adapter);
    }

}
}

Here I have used an AsyncTask to get the JSON object. The results are then displayed in a listview.

Right after String url = c.getString(OBJ_URL);, I have used Log to get check whether the values of url are returned null.

Log.d("URL : ",url);

and it gave me logcat displayed this :

D/URL :: https://searchcode.com/codesearch/view/71123349/
02-23 12:52:31.950 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/46026229/
02-23 12:52:31.950 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/36082147/
02-23 12:52:31.950 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/49572716/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/116169728/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/39926774/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/107357332/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/114502986/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/51628740/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/49574358/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/100236697/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/76176571/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/55589993/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/65881790/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/46430083/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/49572625/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/49572701/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/49574429/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/65259684/
02-23 12:52:31.951 12979-13078/com.example.shaloin.codesearch D/URL :: https://searchcode.com/codesearch/view/119281487/

So, I guess this means that the url is fetched correctly, but isn't displayed for some reasons.

9
  • Are you catching any Exception? and did you check the value of url right after String url = c.getString(OBJ_URL);? Commented Feb 23, 2018 at 6:11
  • are you getting null for url at listView.setOnItemClickListener ? Commented Feb 23, 2018 at 6:22
  • Reason for down-vote? Commented Feb 23, 2018 at 6:32
  • You dont need to set the adapter in onPostExecute[You have not added the function setListAdapter]. You can set the adapter in the activity itself and pass it the values once you receive the data Commented Feb 23, 2018 at 6:46
  • @RajasekaranM, I have a listview in which I display name, language and url and for that I have created a row. The url part remains empty Commented Feb 23, 2018 at 7:18

2 Answers 2

1

Your Code is working fine except change your textview id in ListAdapter in onPostExecute method

Like below..

change this.

  R.id.url 

to

 R.id.urlofcode 

here is your output

enter image description here

And when you click on listview will get those values..

here is Log

D/Output: Name =mathrocks url =Haskell urlcode =https://searchcode.com/codesearch/view/71123349/
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a ton for the help, such a noob mistake!!
1

You should play with the position.

This should work.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // getting values from selected ListItem
        String name = resultsList.get(position).get("name");
        String language = resultsList.get(position).get("language");
        String url = resultsList.get(position).get("url");

        Intent intent=new Intent(getApplicationContext(),
                                    MainActivity.class);
        intent.putExtra(OBJ_NAME, name);
        intent.putExtra(OBJ_LANG,  language);
        intent.putExtra(OBJ_URL, url);
        startActivity(intent);

    }
});

Update

replace below code and let me know if it works.

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // getting values from selected ListItem
        String name = ((TextView) view.findViewById(R.id.nameofrepo))
                .getText().toString();
        String lang=((TextView) view.findViewById(R.id.langofrepo))
                .getText().toString();
        String urlcode = ((TextView) view.findViewById(R.id.url))
                .getText().toString();

        Intent intent=new Intent(getApplicationContext(),         DisplayResults2.class);
        intent.putExtra(OBJ_NAME, name);
        intent.putExtra(OBJ_LANG,lang);
        intent.putExtra(OBJ_URL, urlcode);
        startActivity(intent);

    }
});

3 Comments

Also, I have updated my question, could you please check once
Your questions says Parsing json returns null value. now, what is the problem exactly?
The url which I have shown above, it contains JSON attribute and value pairs. I am trying to display the values of name, language and url. The value of name and language attribute is displayed correctly, but the value of url is not. And I am unable to figure out why!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.