-1

I am facing a problem with getJSONFromUrl(), even though I have add the library. Which I get from [here].(http://www.findjar.com/jar/com/googlecode/json-simple/json-simple/1.1/json-simple-1.1.jar.html)

Below is my Class for getting data from URL in JSON format.

I am not sure why the Problem is appearing. They keep display the message

can't resolve the method getJSONFromUrl

public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

        final String TAG = "AsyncTaskParseJson.java";

        // set your json string url here
        String yourJsonStringUrl = "http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php";

        // contacts JSONArray
        JSONArray dataJsonArr = null;

        @Override
        protected void onPreExecute() {}

        @Override
        protected String doInBackground(String... arg0) {

            try {

                // instantiate our json parser
                JsonParser jParser = new JsonParser();

                // get json string from url
                JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);

                // get the array of users
                dataJsonArr = json.getJSONArray("Users");

                // loop through all users
                for (int i = 0; i < dataJsonArr.length(); i++) {

                    JSONObject c = dataJsonArr.getJSONObject(i);

                    // Storing each json item in variable
                    String firstname = c.getString("firstname");
                    String lastname = c.getString("lastname");
                    String username = c.getString("username");

                    // show the values in our logcat
                    Log.e(TAG, "firstname: " + firstname
                            + ", lastname: " + lastname
                            + ", username: " + username);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String strFromDoInBg) {}
    }

So any help will be appreciated.

4
  • stackoverflow.com/a/18538419 Commented Sep 22, 2015 at 8:20
  • I do apply all these things but still not working. Commented Sep 22, 2015 at 8:31
  • Check do you get the response while calling API? Where you call API I can't find any call http method Commented Sep 22, 2015 at 8:36
  • try this JSONObject jsonObject = new JSONObject("your response string"); JSONArray jsonArray = jsonObject.getJSONArray("Users"); Commented Sep 22, 2015 at 8:42

3 Answers 3

2

here i have checked with working demo you can use in this way as well.

public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

    final String TAG = "AsyncTaskParseJson.java";

    // set your json string url here
    String yourJsonStringUrl = "http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php";

    // contacts JSONArray
    JSONArray dataJsonArr = null;

    @Override
    protected void onPreExecute() {}

    @Override
    protected String doInBackground(String... arg0) {

        try {

            // instantiate our json parser
            //JsonParser jParser = new JsonParser();

            JSONParser jParser = new JSONParser();
            String text = "";
            BufferedReader reader=null;
            try
            {

                // Defined URL  where to send data
                URL url = new URL(yourJsonStringUrl);

                // Send POST data request

                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
               /* OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write( data );
                wr.flush();*/

                // Get the server response

                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line = null;

                // Read Server Response
                while((line = reader.readLine()) != null)
                {
                    // Append server response in string
                    sb.append(line + "\n");
                }


                text = sb.toString();
            }
            catch(Exception ex)
            {

            }
            finally
            {
                try
                {

                    reader.close();
                }

                catch(Exception ex) {}
            }


            // get json string from url
            JSONObject json = (JSONObject) jParser.parse(text);


            // get the array of users
            dataJsonArr = (JSONArray) json.get("Users");

            // loop through all users
            for (int i = 0; i < dataJsonArr.size(); i++) {

                JSONObject c = (JSONObject) dataJsonArr.get(i);

                // Storing each json item in variable
                String firstname = (String) c.get("firstname");
                String lastname = (String) c.get("lastname");
                String username = (String) c.get("username");

                // show the values in our logcat
                Log.e(TAG, "firstname: " + firstname
                        + ", lastname: " + lastname
                        + ", username: " + username);

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String strFromDoInBg) {



    }
}

here is log output

09-22 10:54:49.428  28797-29142/mimg.com.demodrop E/AsyncTaskParseJson.java﹕ firstname: Mike, lastname: Dalisay, username: mike143
09-22 10:54:49.428  28797-29142/mimg.com.demodrop E/AsyncTaskParseJson.java﹕ firstname: Jemski, lastname: Panlilios, username: jemboy09
..................
Sign up to request clarification or add additional context in comments.

1 Comment

@Ando Masahashi Thanks for Good Reply. But i am still facing Error on JSONObject json = (JSONObject) jParser.parse(text);
1

Have a look at the source code for JsonParser.

This class has no method getJSONFromUrl(), so naturally it can't be resolved.

In fact, this method doesn't exist anywhere in the json-simple library.

2 Comments

@Tim Castelijns So is there any easy solution to get Json Result from URL ??
@Mediasoft not that I can see. BTW where does JsonParser jParser = new JsonParser(); this class come from? Is it your own custom parser class? If so, add the code for it please
0

You could write your own JSON fetcher and parser.

public class MyJsonFetcher{

    public String getJsonString(String url){
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

           inputStream = httpEntity.getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }

       try {
           BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
           StringBuilder sBuilder = new StringBuilder();

           String line = null;
           while ((line = bReader.readLine()) != null) {
               sBuilder.append(line + "\n");
           }

            inputStream.close();
            return sBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject getJsonObject(String json){
        try {
            return new JSONObject(json);    
        } catch (JSONException e) {}
        return null;
    }

}

And use it like this:

JSONObject obj = fetcher.getJsonObject(fetcher.getJsonString(my_url));

Codes from this answer

1 Comment

return new JSONArray(json); should be JSONObject(json)?

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.