Ogr*_*gre 6 android google-url-shortener
在尝试自己导入库之后,我终于设法发现我可以使用Google Plugin for Eclipse这样做了.
但是,我似乎无法找到如何在Android上实际使用API的任何示例,至少没有可编译的,因为这些示例中所需的类似乎无法由Eclipse解析,所以我只能假设这些Google Plugin for Eclipse为URL Shortener API导入的库中不存在类.我能找到的最接近的例子就是这里,它似乎是针对谷歌应用引擎,而不是Android,并使用我似乎无法访问的类.
所以问题是,如何在Android应用程序中使用此API来获取URL的缩短版本?我希望使用API密钥代替OAuth.
首先在谷歌控制台上创建一个项目并启用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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后只需执行此asynctask,您将获得一个json响应,其中存在id,这只是缩短的Url.
application:Run Code Online (Sandbox Code Playgroud)<meta-data android:name="com.google.android.urlshortener.API_KEY" android:value="{YOUR_API_KEY}"/>
google-api-client-1.17.0-rc.jar
google-api-client-android-1.17.0-rc.jar
google-api-services-urlshortener-v1-rev22-1.17.0-rc.jar
谷歌-http-client-1.17.0-rc.jar
谷歌-http-client-android-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;
}
}
Run Code Online (Sandbox Code Playgroud)