To shorten the URL using google api
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()开发者_Python百科;
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;
}
}
but the thing is i am getting in Log is java.net.UnknownHostException:www.googleapis.com
You have to modify AndroidManifest.xml to grant permission of "android.permission.INTERNET"
Info from: http://android-er.blogspot.com/2011/09/query-and-parse-google-json.html
This looks a network issue. Use the code given here to access a website using java network api http://www.coderanch.com/t/440335/sockets/java/Access-website-Java-Code This will allow you to track the actual error and once that is fixed you can use any JSON lib to parse the response from server.
Ah, if you're getting UnknownHostException that means your DNS server is not successfully resolving www.googleapis.com. I'd check to make sure that you actually have a working network connection.
精彩评论