How do I use the Google URL Shortener API on Android?

后端 未结 4 973
离开以前
离开以前 2020-12-05 16:38

After much fiddling with trying to import the libraries myself, I finally managed to find out that I can do so using the Google Plugin for Eclipse, here.

However, I

4条回答
  •  自闭症患者
    2020-12-05 17:25

    First create a project on google console and enable url shortner api and get api key and the use the following Asynctask to get shortened url.

     public class newShortAsync extends AsyncTask {
    
            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;
    
    
    
    
            }
        }
    

    and then just execute this asynctask you will get a json responce in which id is present which is nothing but shortened Url.

提交回复
热议问题