fatsecret/fatsecret4j

Please help me get a response

ace-g01d opened this issue · 3 comments

Hello, I am trying to use the API in an android application. I followed your android documentation example, but I am getting the following error for each request I try to send:
[2] 2.onErrorResponse: Error:

I just need help getting a response, any help would be appreciated. I think it might have something to do with oauth, I'm probably missing something but I can't seem to figure it out. Any help would be appreciated. Here is my code:

package com.example.caloriecounterapi;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import com.fatsecret.platform.model.CompactFood;
import com.fatsecret.platform.model.CompactRecipe;
import com.fatsecret.platform.model.Food;
import com.fatsecret.platform.model.Recipe;
import com.fatsecret.platform.services.android.Request;
import com.fatsecret.platform.services.android.ResponseListener;

import java.util.List;

public class MainActivity extends AppCompatActivity {

class Listener implements ResponseListener {

    @Override
    public void onFoodListRespone(com.fatsecret.platform.services.Response<CompactFood> response) {

        System.out.println("TOTAL FOOD ITEMS: " + response.getTotalResults());

        List<CompactFood> foods = response.getResults();
        // ^^ contains summary info about food item

        Log.i("===========FOODS===========", "vvvvvvvvvvvvvvvvvvv");
        for (CompactFood food: foods) {
            Log.i("onFoodListRespone!", food.getName());
        }
    }

    @Override
    public void onFoodResponse(Food food) {
        Log.i("onFoodResponse: ", food.getName());
    }
}

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String key = "I probably shouldn't post my key";
    String secret = "I probably shouldn't post my secret";
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    Listener listener = new Listener();
    //String str_response;

    String query = "pasta";

    Request req = new Request(key, secret, listener);

    //This response contains the list of food items at zeroth page for your query
    req.getFoods(requestQueue, query, 0);

    //This response contains the list of food items at page number 3 for your query
    //If total results are less, then this response will have empty list of the food items
    req.getFoods(requestQueue, query, 3);

    //This food object contains detailed information about the food item
    req.getFood(requestQueue, 29304L);


    Log.i("response!", "received");

}

}

I also tried following an example on the issues forum, but it gives me the error:

2019-06-27 09:11:15.491 30700-30700/com.example.caloriecounterapi I/System.out: Exception: Cleartext HTTP traffic to platform.fatsecret.com not permitted
2019-06-27 09:11:15.491 30700-30700/com.example.caloriecounterapi I/response!: IS NULL :(

Here is the code I used for that one:

    String key = "c92978d965e64f22bd57ad49833efd3d";
    String secret = "cc481c0751314401bc2aa6afec5755cd";
    FatsecretService service = new FatsecretService(key, secret);
    String query = "pasta";
    com.fatsecret.platform.services.Response<CompactFood> response = service.searchFoods(query);
    if (response == null) {
        Log.i("response!", "IS NULL :(");
    }

If you could give me any advice at all on how to get a response for my app, I would greatly appreciate it. Thank you for taking the time to read this and have a good day :)

Hey! Im facing the same issue where you able to fix it?

I was seeing the exact same problem recently (fixed by stackoverflow link below options) and found out that my issue is that "Starting with Android 9 (API level 28), cleartext support is disabled by default."

So if you are using API (SDK) 28 or higher, you will have to have a workaround or you will be getting the following error:

java.io.IOException: Cleartext HTTP traffic to * not permitted

I fixed my issue by following the options given in the stackoverflow answer below, very easy to fix! Just a couple changes to the AndroidManifest and possibly creating an XML file in the resources folder was all. Try each of them and one might work!

Option 3 worked for me to add android:usesCleartextTraffic="true" in the AndroidManifest in the <application ... > part.

https://stackoverflow.com/a/50834600

@ace-g01d @mbotross

I was seeing the exact same problem recently (fixed by stackoverflow link below options) and found out that my issue is that "Starting with Android 9 (API level 28), cleartext support is disabled by default."

So if you are using API (SDK) 28 or higher, you will have to have a workaround or you will be getting the following error:

java.io.IOException: Cleartext HTTP traffic to * not permitted

I fixed my issue by following the options given in the stackoverflow answer below, very easy to fix! Just a couple changes to the AndroidManifest and possibly creating an XML file in the resources folder was all. Try each of them and one might work!

Option 3 worked for me to add android:usesCleartextTraffic="true" in the AndroidManifest in the <application ... > part.

https://stackoverflow.com/a/50834600

@ace-g01d @mbotross

Thanks a lot man worked after a lot of time!!1