-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.

Tim
  • 41,901
  • 18
  • 127
  • 145
Mediasoft
  • 271
  • 2
  • 7
  • 18

3 Answers3

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
..................
Ando Masahashi
  • 3,112
  • 2
  • 24
  • 41
  • **@Ando Masahashi** Thanks for Good Reply. But i am still facing Error on JSONObject json = (JSONObject) jParser.parse(text); – Mediasoft Sep 23 '15 at 02:19
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.

Tim
  • 41,901
  • 18
  • 127
  • 145
  • **@Tim Castelijns** So is there any easy solution to get Json Result from URL ?? – Mediasoft Sep 22 '15 at 08:30
  • 2
    @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 – Tim Sep 22 '15 at 08:33
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

Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129