Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.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 目标主机不能为空JSON请求_Java_Android_Json_Oauth_Yelp - Fatal编程技术网

Java 目标主机不能为空JSON请求

Java 目标主机不能为空JSON请求,java,android,json,oauth,yelp,Java,Android,Json,Oauth,Yelp,我正在尝试对YelpAPI进行查询。然而,我一直得到这个结果 {"error": {"text": "Signature was invalid", "id": "INVALID_SIGNATURE", "description": "Invalid signature. Expected signature base string: GET\u0026https%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch\u0026category_filter%3DAmerican

我正在尝试对YelpAPI进行查询。然而,我一直得到这个结果

{"error": {"text": "Signature was invalid", "id": "INVALID_SIGNATURE", "description": "Invalid signature. Expected signature base string: GET\u0026https%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch\u0026category_filter%3DAmerican%26cc%3DAUll%253D-37.81107631570312%252C144.96785815805197%26limit%3D10%26oauth_consumer_key%3DyiuyLqUyFVE_-0tOCUkPhw%26oauth_nonce%3Db4f1509d-39ef-4206-8620-1d3dd278c842%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1445592027%26oauth_token%3DkrEbYb38zOb6Id9shO9bOMjBWuGhBnWz%26radius_filter%3D2000%26sort%3D1%26term%3DAmerican"}}
这是我生成请求和Oauth签名本身的代码

private static String hmacSha1(String value, String key)
        throws UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeyException {
    String type = "HmacSHA1";
    SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
    Mac mac = Mac.getInstance(type);
    mac.init(secret);
    byte[] bytes = mac.doFinal(value.getBytes());
    return bytesToHex(bytes);
}

private final static char[] hexArray = "0123456789abcdef".toCharArray();

private static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    int v;
    for (int j = 0; j < bytes.length; j++) {
        v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}
我的问题是,我该如何纠正这一点?我是否需要在传递的字符串中的某个位置放置GET\字符?此外,我遵循了错误中声明的格式,因此我的字符串应该通过。希望我能在这方面得到帮助

编辑-我在我的请求行周围放置了一个URL编码器,它成功地将其发送出去。然而,我现在得到了这个信息


目标主机不能为null,也不能在参数中设置。scheme=null,host=null,path=

尝试不使用URLEncoder.encode

String nonsense = UUID.randomUUID().toString();
    Long authSeconds = System.currentTimeMillis()/1000;
    String req = "https://api.yelp.com/v2/search?"+
            "&category_filter=" + chosen+
            "&cc=AU"+
            "ll=" + x + "," + y+
            "&limit=10"+
            "&oauth_consumer_key=" + YELP_CONSUMER_KEY+
            "&oauth_nonce="+nonsense+
            "&oauth_signature_method=HMAC-SHA1"+
            "&oauth_timestamp="+authSeconds+
            "&oauth_token="+YELP_TOKEN+
            "&radius_filter=2000"+
            "&sort=1"+
            "&term=" + chosen;
    String final_req = null;
    try{
        String signature = hmacSha1(req, YELP_TOKEN_SECRET);
        String arg = "&oauth_signature=";
        final_req = req+arg+signature;
    }catch(Exception e){
        Log.d("YELPEXCEPTION",e.toString());
    }
    new GetPlacesJSONFeed().execute(final_req);

如果没有URL编码,我会得到以下信息:{“错误”:{“文本”:“签名无效”,“id”:“无效的签名”,“描述”:"签名无效。预期的签名基本字符串:GET\u0026https%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch\u0026category_筛选器%3dameric%26cc%3DAUll%253D-37.81221290867261%252C144.955616556108%26限制%3D10%26oauth_用户密钥%3dyuyyquyf5-0ockphw%267oauth_nonce%3da3ba3c3c3c3c3d8;-906d-4d-4f-ab6a-ab6a-3effeft8%oauth签名方法hauth%3dauthestamp%3D1445605461%26oauth_令牌%3DKREBYB38ZOB6ID9SHO9BOMJBWWWZ%26radius_筛选器%3D2000%26sort%3D1%26term%3DAmerican“}我可以知道这是传递请求的正确方法还是有其他方法吗?我的请求字符串的格式也是错误消息所希望的。我不知道正确的方法,因为我没有使用YELP API。但是我认为UrlEncode会损坏你的url,YELP API不会识别你的请求和密钥。有没有办法检查在通过发送url之前,结果是否正确?或者是否有使用其他API的示例?
String nonsense = UUID.randomUUID().toString();
    Long authSeconds = System.currentTimeMillis()/1000;
    String req = "https://api.yelp.com/v2/search?"+
            "&category_filter=" + chosen+
            "&cc=AU"+
            "ll=" + x + "," + y+
            "&limit=10"+
            "&oauth_consumer_key=" + YELP_CONSUMER_KEY+
            "&oauth_nonce="+nonsense+
            "&oauth_signature_method=HMAC-SHA1"+
            "&oauth_timestamp="+authSeconds+
            "&oauth_token="+YELP_TOKEN+
            "&radius_filter=2000"+
            "&sort=1"+
            "&term=" + chosen;
    String final_req = null;
    try{
        String signature = hmacSha1(req, YELP_TOKEN_SECRET);
        String arg = "&oauth_signature=";
        final_req = req+arg+signature;
    }catch(Exception e){
        Log.d("YELPEXCEPTION",e.toString());
    }
    new GetPlacesJSONFeed().execute(final_req);