谷歌网址缩短不起作用

时间:2018-04-17 06:16:01

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

您好我正在制作一个应用程序,用户可以将卡发送给其他用户,但是当我点击发送我正在尝试使用网址缩短发送链接但我的网址缩短根本不起作用它总是显示错误消息网址缩短失败 我做了一些关于SO的研究,但没有得到适当的解决方案 我知道有些事情不对,但不知道哪个部分不对 下面是我的网址缩短代码

 public void getShortenedUrl(String url, final int sendType){
            try {
                if (NetworkInfo.isNetworkAvailable(getActivity())) {
                    final boolean isMyCard=getArguments().getBoolean("isMyCard");
                    final String sharedTo=ceShareVia.getText().toString();
                    final String[] addresses = sharedTo.split(",");
                    LongUrl data = new LongUrl(url);
                    Call<LongUrl> cardResponseCall = ApiClientMain.getApiClient("https://www.googleapis.com/urlshortener/v1/")
                            .getShortenedUrl("application/json",ApplicationData.GOOGLE_URL_SHORTENER_KEY, data);
                    pbShare.setVisibility(View.VISIBLE);
                    cardResponseCall.enqueue(new retrofit2.Callback<LongUrl>() {
                        @Override
                        public void onResponse(Call<LongUrl> call, Response<LongUrl> response) {
                            pbShare.setVisibility(View.GONE);
                            if (response != null && response.body() != null) {
                                String url = response.body().getId();
                                String message;
                                if(isMyCard) {
                                     message = "Hello," + "\n"
                                            + "Thanks for connecting!\n\n" + introMessage + "\n\n" + url + "\n\nRegards,\n"
                                            + preferenceManager.getPreferenceValues(PreferenceConstant.PERSON_NAME)
                                            + "\n" + "Have a nice day!";
                                }else {
                                    message = "Hello," + "\n"
                                            + "Thanks for connecting!\n\n" + introMessageOther + "\n\n" + url + "\n\nRegards,\n"
                                            + preferenceManager.getPreferenceValues(PreferenceConstant.PERSON_NAME)
                                            + "\n" + "Have a nice day!";
                                }


                                switch (sendType){
                                    case 0:
                                        sendEmail(addresses,"My Business Card",message);
                                        break;
                                    case 1:
                                        sendSms(sharedTo,message);
                                        break;
                                    case 2:
                                        shareTextUrl(message);
                                        break;
                                }
                            }
                            else {
                                Toast.makeText(getActivity(),"Url shortening failed",Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<LongUrl> call, Throwable t) {
                            pbShare.setVisibility(View.GONE);

                            Toast.makeText(getActivity(),"Url shortening failed",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

这里是Long Url的模型类

public class LongUrl {
    @SerializedName("longUrl")
    @Expose
    private String longUrl;
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("kind")
    @Expose
    private String kind;

    public String getLongUrl() {
        return longUrl;
    }

    public void setLongUrl(String longUrl) {
        this.longUrl = longUrl;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public LongUrl(){

    }

    public LongUrl(String longUrl){
        this.longUrl = longUrl;
    }
}

1 个答案:

答案 0 :(得分:5)

Google URL Shortener已关闭。

官方发布

从2018年3月30日开始,我们将拒绝支持goo.gl网址缩短。从2018年4月13日起,只有现有用户才能在goo.gl控制台上创建短链接。您将能够查看您的分析数据并以csv格式下载短链接信息长达一年,直到2019年3月30日,我们将停止goo.gl.以前创建的链接将继续重定向到其预定目的地。

Read this Blog for More Details.

如果这是一个新项目,则无法使用API​​创建缩短网址