后台任务使用Fatsecret rest api返回空字符串

时间:2017-05-10 19:48:16

标签: java android rest

我试图使用Fatsecret Rest api方法foods.search来返回基于与参数中搜索表达式相关的食物对象的营养价值。我遇到的问题是我的后台任务。< / p>

    void requester(){

    class GetRequest extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... params) {
            String result="";
            BufferedReader bufferedReader=null;
            try {

             /* Global string variable that contains the request */

                URL url = new URL(apiUrle); 



                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                StringBuilder sb = new StringBuilder();

                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String json;
                while((json = bufferedReader.readLine())!= null){
                    sb.append(json+"\n");
                }

                result =  sb.toString();


        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.getMessage();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.getMessage();
        }



            return result;

        }

         @Override
            protected void onPostExecute(String result) {

                Log.d("YOUR RESULT", result+"230");/* result string appears empty in logcat since it only displays string 230*/
            }

    }
    GetRequest ret = new GetRequest();
    ret.execute();
}

主要问题是返回的字符串(结果)是空的(我将解析为json的字符串)。我通过在java应用程序上使用相同的值来验证它不是错误的使用者密钥或秘密在我的netbeansIDE上我没有问题加上我使用相同的方法来构建请求。 我还在浏览器中输入了apiUrle值,得到了我预期的结果。

   { "foods": { "food": [ {"food_description": "Per 101g - Calories: 197kcal | Fat: 7.79g | Carbs: 0.00g | Protein: 29.80g", "food_id": "1641", "food_name": "Chicken Breast", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-breast-ns-as-to-skin-eaten" }, {"food_description": "Per 100g - Calories: 110kcal | Fat: 1.24g | Carbs: 0.00g | Protein: 23.09g", "food_id": "4881229", "food_name": "Skinless Chicken Breast", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-breast-skinless" }, {"food_description": "Per 101g - Calories: 239kcal | Fat: 13.60g | Carbs: 0.00g | Protein: 27.30g", "food_id": "448901", "food_name": "Grilled Chicken", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-grilled-ns-as-to-skin-eaten" }, {"food_description": "Per 101g - Calories: 247kcal | Fat: 15.49g | Carbs: 0.00g | Protein: 25.06g", "food_id": "1695", "food_name": "Chicken Thigh", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-thigh-ns-as-to-skin-eaten" }, {"brand_name": "Valbest", "food_description": "Per 4 oz - Calories: 130kcal | Fat: 3.00g | Carbs: 0.00g | Protein: 26.00g", "food_id": "3946778", "food_name": "Chicken Breast", "food_type": "Brand", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/valbest\/chicken-breast" }, {"food_description": "Per 101g - Calories: 216kcal | Fat: 11.15g | Carbs: 0.00g | Protein: 27.03g", "food_id": "1677", "food_name": "Chicken Drumstick", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-drumstick-ns-as-to-skin-eaten" }, {"food_description": "Per 101g - Calories: 239kcal | Fat: 13.60g | Carbs: 0.00g | Protein: 27.30g", "food_id": "419178", "food_name": "Rotisserie Chicken", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-rotisserie-ns-as-to-skin-eaten" }, {"food_description": "Per 101g - Calories: 190kcal | Fat: 7.41g | Carbs: 0.00g | Protein: 28.93g", "food_id": "1628", "food_name": "Roasted Broiled or Baked Chicken (Skin Not Eaten)", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-roasted-broiled-or-baked-skin-not-eaten" }, {"food_description": "Per 96g - Calories: 279kcal | Fat: 16.60g | Carbs: 9.59g | Protein: 21.45g", "food_id": "1636", "food_name": "Baked or Fried Coated Chicken with Skin (Skin\/Coating Eaten)", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-coated-baked-or-fried-prepared-with-skin-skin-coating-eaten" }, {"food_description": "Per 100g - Calories: 237kcal | Fat: 11.89g | Carbs: 0.00g | Protein: 30.42g", "food_id": "34511", "food_name": "Chicken Meat (Stewing, Stewed, Cooked)", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/usda\/chicken-meat-(stewing-stewed-cooked)" } ], "max_results": "10", "page_number": "0", "total_results": "5362" }}

否则是的,我不知道我做错了什么,所以任何见解都会深深体会到

0 个答案:

没有答案