【问题标题】:google url shortening not working谷歌网址缩短不起作用
【发布时间】:2018-04-17 06:16:01
【问题描述】:

嗨,我正在制作一个应用程序,用户可以在其中向其他用户发送卡片,但是当我单击发送时,我试图使用 url 缩短发送链接,但我的 url 缩短根本不起作用,它总是显示错误消息 url缩短失败 我对 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;
    }
}

【问题讨论】:

  • 响应或 response.body。它们是空的吗?您可能想在开发时尝试一些更有帮助的消息。也可以尝试单步执行代码。
  • 你认为我哪里错了
  • 您应该检查Throwable t 以了解错误原因

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


【解决方案1】:

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 创建缩短 URL

【讨论】:

  • 我不知道如何使用新的 firebase api 创建缩短的 url
  • 试试看。我们通过使用或尝试来学习。或者尝试其他一些 URL Shortners,例如 Bitly 或 OW.LY
  • 阅读开发者博客
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多