-1

I am newbie in android programmimng and i am trying to create an app for my food blog.I found this json paring tutorial on internet. when i put my yql query into the code,it gives me an error " no value for json". when i tried to fix them,i found that error is in my jsonobject varible which requires a string "json" like this...

how can i fix this error... help me

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.listview_main);
    // Execute DownloadJSON AsyncTask
    new DownloadJSON().execute();
}

// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(MainActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Welcome to Food-n-moreblog App");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create the array
        arraylist = new ArrayList<HashMap<String, String>>();
        // YQL JSON URL
        String url = "https://query.yahooapis.com/v1/public/yql?q=SELECT%20title%20FROM%20atom%20WHERE%20url%3D%22http%3A%2F%2Ffood-n-moreblog.blogspot.com%2Ffeeds%2Fposts%2Fdefault%22%20LIMIT%205%20&format=json&callback=";

        try {
            // Retrive JSON Objects from the given URL in JSONfunctions.class
            JSONObject json_data = JSONfunctions.getJSONfromURL(url);
            JSONObject json_query = json_data.getJSONObject("title");
            JSONObject json_results = json_query.getJSONObject("results");
            JSONObject json_json_result = json_results
                    .getJSONObject("entry");
            JSONArray json_result = json_json_result.getJSONArray("items");

            for (int i = 0; i < json_result.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject c = json_result.getJSONObject(i);
                JSONObject vo = c.getJSONObject("entry");
                map.put("title", vo.optString("title"));
               /* map.put("description", vo.optString("description"));
                JSONObject il = vo.getJSONObject("imageLinks");
                map.put("thumbnail", il.optString("thumbnail"));*/
                arraylist.add(map);
            }

        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(MainActivity.this, arraylist);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}

}

Here is my Json

    {
     "query": {
     "count": 5,
      "created": "2015-12-16T10:00:40Z",
      "lang": "en-US",
      "results": {
       "entry": [
        {
         "title": {
          "type": "text",
        "content": "Recipe : home-made Curd | how to make yoghurt at         home"
         }
        },
        {
        "title": {
         "type": "text",
          "content": "Recipe: Classic Vanilla Icecream | how to make         eggless vanilla icecream at home"
         }
         },
        {
        "title": {
          "type": "text",
         "content": "Recipe : Matar Mushroom in white gravy | Peas        mushroom in white sauce"
        }
        },
       {
        "title": {
        "type": "text",
         "content": "Recipe : Mississippi Mud and Chocolate Frappe`"
        }
       },
      {
       "title": {
       "type": "text",
       "content": "Recipe : Moong Dal Khichdi | how to make yellow       gujrati khichadi"
       }
       }
      ]
      }
      } 
     }
priyaj
  • 1

5 Answers5

0

There are several errors

  1. You never execute a http request (in the code you provide)

  2. I think json_query must come from query instead of title So replace

    JSONObject json_query = json_data.getJSONObject("title");
    

    by

    JSONObject json_query = json_data.getJSONObject("query");
    
  3. results is not a jsonObject but a JsonArray so replace

    JSONObject json_results = json_query.getJSONObject("results");
    

    by

    JSONArray json_results = json_query.getJSONArray("results");
    
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
0

Alright below is the JSON parsed -

String urlString = "https://query.yahooapis.com/v1/public/yql?q=SELECT%20title%20FROM%20atom%20WHERE%20url%3D%22http%3A%2F%2Ffood-n-moreblog.blogspot.com%2Ffeeds%2Fposts%2Fdefault%22%20LIMIT%205%20&format=json&callback=";

    URL url = null;
    try {
        url = new URL(urlString);
        HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
        InputStream in  = httpconn.getInputStream();

        int ch;
        StringBuffer buffer  = new StringBuffer();

        while( (ch = in.read()) != -1){
            buffer.append((char)ch);
        }

        JSONObject jObj = new JSONObject(buffer.toString());
        JSONObject jQuery = jObj.getJSONObject("query");
        JSONObject jResult = jQuery.getJSONObject("results");
        JSONArray jEntryArray = jResult.getJSONArray("entry");

        for(int i=0; i<jEntryArray.length()-1; i++){
            JSONObject jEntryObj = jEntryArray.getJSONObject(i);
            JSONObject jTitle = jEntryObj.getJSONObject("title");
            String Content = jTitle.getString("content");
        }

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

Make sure u do above code in AynTask and not on main thread. U can store the Content string in ur data structure and use it later.

kevz
  • 2,727
  • 14
  • 39
0

as per your json response...entry is an array not an object...so your code should be

 JSONObject json_data = JSONfunctions.getJSONfromURL(url);
 JSONObject json_results = json_query.getJSONObject("results");
 JSONObject json_array = json_results.getJSONArray(entry);

 for (int i = 0; i < json_array.length(); i++) {
   JSONObject obj = json_array.getJsonObject(i);

   JSONObject title = obj.getJsonObject("titile");

   //get title like
   String type = obj.getString("type");
 }
H Raval
  • 1,903
  • 17
  • 39
0

You are parsing in wrong way:

 JSONObject json_data = JSONfunctions.getJSONfromURL(url); //suppose it is ok
//                JSONObject json_query = json_data.getJSONObject("title"); //this is wrong -> you need get parent object of title before get it, so replace by this code:
JSONObject json_query = json_data.getJSONObject("query");

                JSONObject json_results = json_query.getJSONObject("results"); //ok
               // JSONObject json_json_result = json_results.getJSONObject("entry"); // wrong because entry is a array of objects, so replace it by this code:
                JSONArray json_entry = json_results.getJSONArray("entry");

                //JSONArray json_result = json_json_result.getJSONArray("items"); //wrong -> because have not any items key in json

for (int i=0; i < json_entry.length(); i++){
   JSONObject json_title = json_entry.get(i);
   String type = json_title.getString("type");
   String content = json_title.getString("content");
   //next step of you for above data
}
GiapLee
  • 436
  • 2
  • 8
-1
  your Activity:
      HttpClient httpClient = new DefaultHttpClient();
            // Creating HTTP Post
            HttpPost httpPost = new HttpPost(
                    your url);
            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            //nameValuePair.add(new BasicNameValuePair("userid", userid));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            HttpResponse response = httpClient.execute(httpPost);
            String strRet=HttpHelper.request(response);
            JSONObject object = new JSONObject(strRet);
          //here you have to parse or retrive the values 

//httphelper.java:
  public class HttpHelper {

public static String request(HttpResponse response){
    String result = "";
    try{
        InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null){
            str.append(line + "\n");
        }
        in.close();
        result = str.toString();
    }catch(Exception ex){
        result = "Error";
    }
    return result;
}

}

MurugananthamS
  • 2,395
  • 4
  • 20
  • 49