谷歌网址缩短为ip提供网址错误

时间:2017-04-07 08:27:04

标签: java android url-shortener google-url-shortener

我正在使用Google Url缩短API。 缩短网址“http://demos.companyname.com:1339/

但是当我用“http://demos.companyname.com:1339/”替换此网址“http://36.186.69.8:1339/”时,它会返回错误。

如何解决这个问题?

我的网址缩短代码为:

public JSONObject getJSONFromUrl(String address,String longUrl){

    // Making HTTP request
    try {
        // DefaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(address);

        httpPost.setEntity(new StringEntity("{\"longUrl\":\""+ longUrl+"\"}"));
        httpPost.setHeader("Content-Type", "application/json");
        HttpResponse httpResponse = httpClient.execute(httpPost);

        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");

        }
        is.close();
        Log.e("getJSONFromUrl: ", sb.toString());
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // Parse the String to a JSON Object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    // Return JSON String
    return jObj;

}

错误回复:

   {"error":{"errors":
         [{"domain":"global","reason":"invalid","message":"InvalidValue","locationType":"parameter","location":"resource.longUrl"}],"code":00,"message":"Invalid Value"}}

1 个答案:

答案 0 :(得分:0)

据我所知,google URL shortener不适用于IP, 它仅适用于映射的URL。

查看以下链接,了解Google是否可以缩短您的网址。

https://goo.gl/