为了缩短使用谷歌API的网址 [英] To shorten the URL using google api

查看:267
本文介绍了为了缩短使用谷歌API的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个例子来进行网址缩短。但不能能够做到这一点,这是我的code

Hi i am using an example to make a url shorten. But cant able to do that, here's my code

package com.tinyurl;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.api.client.googleapis.GoogleHeaders;
import com.google.api.client.googleapis.GoogleTransport;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonHttpContent;
import com.google.api.client.json.JsonHttpParser;
import com.google.api.client.util.GenericData;
public class tinyurl extends Activity implements OnClickListener {
EditText original;
TextView txt;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btn = (Button) findViewById(R.id.Button01);

    original = (EditText) findViewById(R.id.edittext);
    txt = (TextView) findViewById(R.id.TextView03);
    btn.setOnClickListener(this);
}


@Override
public void onClick(View v) {

    final String GOOGL_URL = "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyAau1E_WrYwMiTNqhK5hgH0tyWudyahbOI";
    String tinyUrl = null;
    String original = "http://www.google.com/";
    HttpTransport transport = GoogleTransport.create();
    GoogleHeaders defaultHeaders = new GoogleHeaders();
    transport.defaultHeaders = defaultHeaders;
    transport.defaultHeaders.put("Content-Type", "application/json");
    transport.addParser(new JsonHttpParser());
    HttpRequest request = transport.buildPostRequest();
    request.setUrl(GOOGL_URL);
    GenericData data = new GenericData();
    // data.put("longUrl", "http://www.google.com/");
    data.put("longUrl", original);
    JsonHttpContent content = new JsonHttpContent();

    content.data = data;
    request.content = content;

    HttpResponse response = null;
    try {
        response = request.execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("eception occured", e.toString());
    }
    Result result = null;
    try {
        result = response.parseAs(Result.class);
        Log.d("TinyUrl", result.shortUrl.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public class Result extends GenericJson {

    public String shortUrl;

}

}

但事情是我在日志越来越是的java.net.UnknownHostException:www.googleapis.com

but the thing is i am getting in Log is java.net.UnknownHostException:www.googleapis.com

推荐答案

您必须修改AndroidManifest.xml中授予的权限android.permission.INTERNET对

You have to modify AndroidManifest.xml to grant permission of "android.permission.INTERNET"

从信息:的http://机器人-er.blogspot.com/2011/09/query-and-parse-google-json.html

这篇关于为了缩短使用谷歌API的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆