Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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
Android 如何删除预期的BEGIN_对象,但在第1行第1列路径$i处存在字符串?_Android_Rest_Retrofit2 - Fatal编程技术网

Android 如何删除预期的BEGIN_对象,但在第1行第1列路径$i处存在字符串?

Android 如何删除预期的BEGIN_对象,但在第1行第1列路径$i处存在字符串?,android,rest,retrofit2,Android,Rest,Retrofit2,我正在研究FatSecretAPI并试图获得食谱列表 对于需要发送以下参数的访问: oauth_consumer_key String --Your API key when you registered as a developer oauth_signature_method String-- The method used to generate the signature (only HMAC-SHA1 is supported) oauth_timestamp Int--

我正在研究FatSecretAPI并试图获得食谱列表

对于需要发送以下参数的访问:

oauth_consumer_key  String  --Your API key when you registered as a developer
oauth_signature_method  String--    The method used to generate the signature (only HMAC-SHA1 is supported)
oauth_timestamp Int--   The date and time, expressed in the number of seconds since January 1, 1970 00:00:00 GMT. The timestamp value must be a positive integer and must be equal or greater than the timestamp used in previous requests
oauth_nonce String  --A randomly generated string for a request that can be combined with the timestamp to produce a unique value
oauth_version   String--    MUST be "1.0"
oauth_signature String--    The signature, a consistent reproducible concatenation of the request elements into a single string. The string is used as an input in hashing or signing algorithms.
method  String  --MUST be "recipes.search"
作为回应,我会:

recipe_id – the unique recipe identifier.
recipe_name – the name of the recipe.
recipe_url – URL of this recipe item on www.fatsecret.com.
recipe_description – A short description of the recipe.
recipe_image – URL of this recipe item's default image, only if this is available
我有一个json响应,如下所示:

  {  
   "recipes":{  
      "recipe":{  
         "recipe_description":"Healthy fish with a tasty sauce.",
         "recipe_id":"91",
         "recipe_image":"http:\/\/www.fatsecret.com\/static\/recipe\/bf0c5912-9cf8-4e7a-b07a-6703c4b77082.jpg",
         "recipe_name":"Baked Lemon Snapper",
         "recipe_url":"http:\/\/www.fatsecret.com\/recipes\/baked-lemon-snapper\/Default.aspx"
      }
   }
}
我的心看起来像:

公共接口MyCallApi{

String BASE_URL = "http://platform.fatsecret.com/";


@POST("rest/server.api/")
Call<Recipes> getRecipes(@Query("oauth_consumer_key") String oauth_consumer_key,
                         @Query("oauth_signature_method") String oauth_signature_method,
                         @Query("oauth_timestamp") int oauth_timestamp,
                         @Query("oauth_nonce") String oauth_nonce,
                         @Query("oauth_version") String oauth_version,
                         @Query("oauth_signature") String oauth_signature,
                         @Query("method") String method);
对于recipes对象中的数据:

public class Recipe {
    @SerializedName("recipe_name")
    @Expose
    private String recipe_name;
    @SerializedName("recipe_url")
    @Expose
    private String recipe_url;
    @SerializedName("recipe_description")
    @Expose
    private String recipe_description;
    @SerializedName("recipe_image")
    @Expose
    private String recipe_image;
    @SerializedName("recipe_id")
    @Expose
    private String recipe_id;

    public String getRecipe_name() {
        return recipe_name;
    }

    public void setRecipe_name(String recipe_name) {
        this.recipe_name = recipe_name;
    }

    public String getRecipe_url() {
        return recipe_url;
    }

    public void setRecipe_url(String recipe_url) {
        this.recipe_url = recipe_url;
    }

    public String getRecipe_description() {
        return recipe_description;
    }

    public void setRecipe_description(String recipe_description) {
        this.recipe_description = recipe_description;
    }

    public String getRecipe_image() {
        return recipe_image;
    }

    public void setRecipe_image(String recipe_image) {
        this.recipe_image = recipe_image;
    }

    public String getRecipe_id() {
        return recipe_id;
    }

    public void setRecipe_id(String recipe_id) {
        this.recipe_id = recipe_id;
    }

    @NonNull
    @Override
    public String toString() {
        return "ClassPojo [recipe_name = " + recipe_name + "," +
                " recipe_url = " + recipe_url + ", " +
                "recipe_description = " + recipe_description + "," +
                " recipe_image = " + recipe_image + "," +
                " recipe_id = " + recipe_id + "]";
    }
}
改造实施包括:

public class RecipeActivity extends AppCompatActivity {


final static private String APP_METHOD = "GET";
final static private String APP_KEY = "here api key";
final static private String APP_SECRET = "ssecret key";
final static private String APP_URL = "http://platform.fatsecret.com/rest/server.api";
private static final String HMAC_SHA1_ALGORITHM = "HMAC-SHA1";


private static String paramify(String[] params) {
    String[] p = Arrays.copyOf(params, params.length);
    Arrays.sort(p);
    return join(p, "&");
}

private static String join(String[] array, String separator) {
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < array.length; i++) {
        if (i > 0)
            b.append(separator);
        b.append(array[i]);
    }
    return b.toString();
}


private static String nonce() {
    Random r = new Random();
    StringBuilder n = new StringBuilder();
    for (int i = 0; i < r.nextInt(8) + 2; i++)
        n.append(r.nextInt(26) + 'a');
    return n.toString();
}

Long tsLong = System.currentTimeMillis() / 1000;
int ts = Integer.parseInt(tsLong.toString());

private static String[] generateOauthParams() {
    return new String[]{
            "oauth_consumer_key=" + APP_KEY,
            "oauth_signature_method=HMAC-SHA1",
            "oauth_timestamp=" +
                    Long.valueOf(System.currentTimeMillis() * 2).toString(),
            "oauth_nonce=" + nonce(),
            "oauth_version=1.0",
            "format=json"};
}

private static String signature(String[] params) {
    String[] p = {RecipeActivity.APP_METHOD, Uri.encode(RecipeActivity.APP_URL), Uri.encode(paramify(params))};
    String s = join(p, "&");
    SecretKey sk = new SecretKeySpec(APP_SECRET.getBytes(), HMAC_SHA1_ALGORITHM);
    try {
        Mac m = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        m.init(sk);
        return Uri.encode(new String(Base64.encode(m.doFinal(s.getBytes()), Base64.DEFAULT)).trim());
    } catch (java.security.NoSuchAlgorithmException | java.security.InvalidKeyException e) {
        Log.w("FatSecret_TEST FAIL", e.getMessage());
        return null;
    }
}


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


    Gson gson = new GsonBuilder()
            .setLenient()
            .create();


    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(MyCallApi.BASE_URL)
            .build();

    MyCallApi myCallApi = retrofit.create(MyCallApi.class);
    Call<Recipes> call = myCallApi.getRecipes("c30f50a1c5474070b4db11a506d99666",
            "HMAC-SHA1", ts
            , nonce()
            , "1.0", signature(generateOauthParams()),
            "recipes.search");

    call.enqueue(new Callback<Recipes>() {
        @Override
        public void onResponse(@NonNull Call<Recipes> call, @NonNull Response<Recipes> response) {
            Log.i("if works", response.toString());
        }

        @Override
        public void onFailure(@NonNull Call<Recipes> call, @NonNull Throwable t) {
            Log.i("if not", t.getMessage());
        }
    });

}
公共类RecipeActivity扩展了AppCompativity{
最终静态私有字符串APP_METHOD=“GET”;
最终静态私有字符串APP_KEY=“此处为api KEY”;
最终静态私有字符串APP_SECRET=“ssecret key”;
最终静态私有字符串APP_URL=”http://platform.fatsecret.com/rest/server.api";
私有静态最终字符串HMAC_SHA1_ALGORITHM=“HMAC-SHA1”;
私有静态字符串参数化(字符串[]参数){
字符串[]p=Arrays.copyOf(params,params.length);
数组排序(p);
返回连接(p,“&”);
}
私有静态字符串联接(字符串[]数组,字符串分隔符){
StringBuilder b=新的StringBuilder();
for(int i=0;i0)
b、 附加(分隔符);
b、 追加(数组[i]);
}
返回b.toString();
}
私有静态字符串nonce(){
随机r=新随机();
StringBuilder n=新的StringBuilder();
对于(int i=0;i
}


但是我得到了一个被问到最多的错误。我自己无法解决它。我是一个新的改装者。我不知道我做错了什么,哪里做错了,请检查一下实现并指导我成功获得响应。记住我必须在改装请求中发送这些参数。*

再制作一个类似这样的pojo类

public class RecipeResponse{

@SerializedName("recipes")
private Recipes recipes;

public void setRecipes(Recipes recipes){
    this.recipes = recipes;
}

public Recipes getRecipes(){
    return recipes;
}
}

在使用..时进行更改api调用之后

Call<RecipeResponse> 
呼叫

再制作一个这样的pojo类

public class RecipeResponse{

@SerializedName("recipes")
private Recipes recipes;

public void setRecipes(Recipes recipes){
    this.recipes = recipes;
}

public Recipes getRecipes(){
    return recipes;
}
}

在使用..时进行更改api调用之后

Call<RecipeResponse> 
呼叫

您是否使用了oAuth 1.0?在传递“菜谱”之前,您可能会错过对json对象的解析内部对象。@subinbab在所需参数中,我只需要根据他们的官方文档指定oAuth 1.0版。请在下面的链接上查看我的答案,需要帮助吗?我将详细回答您的问题@JayDangar,那么如何解决这个问题呢?您是否使用了oAuth 1.0?在传递“配方”之前,您可能会错过解析json对象的机会内部对象。@subinbab在必需的参数中,我只需要根据他们的官方文档指定oAuth版本1.0。请检查我在以下链接上的答案,需要帮助吗?我将详细回答您的问题@JayDangar,那么如何解决?