Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Fatsecret rest api返回空字符串的后台任务_Java_Android_Rest - Fatal编程技术网

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

Java 使用Fatsecret rest api返回空字符串的后台任务,java,android,rest,Java,Android,Rest,我正在尝试使用Fatsecret Rest api方法foods.search根据与参数中搜索表达式相关的食物对象返回营养值。我遇到的问题是在我的后台任务中 void requester(){ class GetRequest extends AsyncTask<String,Void,String>{ @Override protected String doInBackground(String... params) {

我正在尝试使用Fatsecret Rest api方法foods.search根据与参数中搜索表达式相关的食物对象返回营养值。我遇到的问题是在我的后台任务中

    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();
}

否则,是的,我不知道我做错了什么,因此任何洞察都将受到高度赞赏

我猜会抛出异常。您可以使用
e.printStackTrace()
打印它
e.getMessage()
返回字符串,但它不打印任何内容。是的,它引发了未知主机异常。。
   { "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" }}