在尝试自己导入库之后,我终于设法发现我可以使用Google Plugin for Eclipse here来做到这一点。
然而,我似乎无法找到任何关于如何在Android上实际使用API的示例,至少没有可编译的,因为这些示例中所需的类似乎无法由Eclipse解析,所以我只能假设这些类不存在于Google Plugin for Eclipse为URL Shortener API导入的库中。我能找到的最接近的示例是here,它似乎适用于Google App Engine,而不是Android,并且使用了我似乎无法访问的类。
所以问题是,如何在Android应用程序中使用此API来获取URL的缩短版本?我希望使用API密钥代替OAuth。
答案 0 :(得分:7)
首先在Google控制台上创建一个项目并启用url shortner api并获取api密钥,并使用以下Asynctask来缩短网址。
public class newShortAsync extends AsyncTask<Void,Void,String> {
String longUrl="http://stackoverflow.com/questions/18372672/how-do-i-use-the-google-url-shortener-api-on-android/20406915";
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressBar.setVisibility(View.GONE);
System.out.println("JSON RESP:" + s);
String response=s;
try {
JSONObject jsonObject=new JSONObject(response);
id=jsonObject.getString("id");
System.out.println("ID:"+id);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... params) {
BufferedReader reader;
StringBuffer buffer;
String res=null;
String json = "{\"longUrl\": \""+longUrl+"\"}";
try {
URL url = new URL("https://www.googleapis.com/urlshortener/v1/url?key=YOUR_API_KEY");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(40000);
con.setConnectTimeout(40000);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(json);
writer.flush();
writer.close();
os.close();
int status=con.getResponseCode();
InputStream inputStream;
if(status==HttpURLConnection.HTTP_OK)
inputStream=con.getInputStream();
else
inputStream = con.getErrorStream();
reader= new BufferedReader(new InputStreamReader(inputStream));
buffer= new StringBuffer();
String line="";
while((line=reader.readLine())!=null)
{
buffer.append(line);
}
res= buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
}
然后只需执行此asynctask,您将获得一个json响应,其中存在id,这只是缩短的Url。
答案 1 :(得分:4)
application
节点中的清单:<meta-data android:name="com.google.android.urlshortener.API_KEY" android:value="{YOUR_API_KEY}"/>
谷歌-API的客户端 - 1.17.0-rc.jar
谷歌-API的客户端 - 机器人-1.17.0-rc.jar
谷歌-API服务-urlshortener-V1-rev22-1.17.0-rc.jar
谷歌-HTTP-客户1.17.0-rc.jar
谷歌-HTTP-客户机器人-1.17.0-rc.jar
方法:
String shorten(String longUrl){
Urlshortener.Builder builder = new Urlshortener.Builder (AndroidHttp.newCompatibleTransport(), AndroidJsonFactory.getDefaultInstance(), null);
Urlshortener urlshortener = builder.build();
com.google.api.services.urlshortener.model.Url url = new Url();
url.setLongUrl(longUrl);
try {
url = urlshortener.url().insert(url).execute();
return url.getId();
} catch (IOException e) {
return null;
}
}
答案 2 :(得分:4)
现在谷歌更短的api需要工作的关键。我尝试在清单中设置密钥,但它不起作用。键应该由函数库设置。
Urlshortener.Builder builder = new Urlshortener.Builder (AndroidHttp.newCompatibleTransport(),
AndroidJsonFactory.getDefaultInstance(), null);
Urlshortener urlshortener = builder.build();
com.google.api.services.urlshortener.model.Url url = new com.google.api.services.urlshortener.model.Url();
url.setLongUrl(longUrl);
try {
Urlshortener.Url.Insert insert=urlshortener.url().insert(url);
insert.setKey("Your API KEY");
url = insert.execute();
return url.getId();
} catch (IOException e) {
LogUtil.e(TAG, Log.getStackTraceString(e));
return null;
}
答案 3 :(得分:0)
你也可以使用gradle
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.apis:google-api-services-urlshortener:v1-rev47-1.22.0'
}