如何在 Android 应用程序中集成 Google Reader?

发布于 2024-10-31 06:47:47 字数 58 浏览 0 评论 0原文

我想将 Google Reader 集成到我的 Android 应用程序中。 请帮助我如何做到这一点?

I want to integrate Google Reader in my android application.
Please help me on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2024-11-07 06:47:47

希望这有帮助:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try{  
        String auth = getGoogleAuthKey("<your gmail email address>","<your password>");
        String token = getGoogleToken(auth); 
        String result = postToGoogleReader(token, auth); 
        TextView tv = (TextView) findViewById(R.id.textView1); 
        tv.setText(result); 
    }catch(Exception e){
    }

}

protected String getGoogleAuthKey(String _USERNAME, String _PASSWORD) {
    String responseString = ""; 
    URL url;
    try {
        //open the connection 
        url = new URL("https://www.google.com/accounts/ClientLogin");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);  

        //create the body
        StringBuilder sb = new StringBuilder();
        sb.append("accountType="); 
        sb.append("GOOGLE"); 
        sb.append("&Email=");
        sb.append(_USERNAME);
        sb.append("&Passwd=");
        sb.append(_PASSWORD);
        sb.append("&service="); 
        sb.append("reader");
        sb.append("&source=");    
        sb.append("<your app name>"); 

        //make a request and retrieve results
        OutputStream outputStream = urlConnection.getOutputStream();  
        outputStream.write(sb.toString().getBytes("UTF-8"));
        outputStream.close(); 
        sb = null; 

        int responseCode = urlConnection.getResponseCode();
        InputStream inputStream;  
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inputStream = urlConnection.getInputStream();
            responseString = convertStreamToString(inputStream);
            String _AUTHKEY = responseString.substring(responseString.indexOf("Auth="), responseString.length());
            responseString = _AUTHKEY.replace( "Auth=","" );
            inputStream.close();
            urlConnection.disconnect(); 
        }else {  
            urlConnection.disconnect(); 
            return "error"; 
        }
    } catch (Exception e) {
        return e.getMessage();  
    }
    Log.d("GoogleReader", "Auth.a=" + responseString); 
    return responseString.trim();
}

public String getGoogleToken(String authorization) {
    final String googleReaderTokenUrl = "https://www.google.com/reader/api/0/token"; 
    String responseString = ""; 
    URL url;

    try {
        //open the connection 
        url = new URL(googleReaderTokenUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        Log.d("GoogleReader", "Auth.b=" + authorization); 
        urlConnection.addRequestProperty("Authorization", "GoogleLogin auth=" + authorization); 
        urlConnection.setRequestMethod("GET");
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlendcoded");
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);

        try {
             InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
             responseString = convertStreamToString(inputStream);
        }catch(Exception e){
            int responseCode = urlConnection.getResponseCode();
               return Integer.toString(responseCode); 
            //return e.getMessage(); 
        }finally {
             urlConnection.disconnect();
        }
    } catch (Exception e) {
        return e.getMessage(); 
    }
    return responseString; 
}

public String postToGoogleReader(String token, String authorization){
    final String googleAuthUrl = "http://www.google.com/reader/api/0/item/edit"; 
    String responseString = "";  
    URL url;
    try { 

        //create the body
        StringBuilder sb = new StringBuilder();
        sb.append("accountType=");
        sb.append("GOOGLE");
        sb.append("&snippet=");
        sb.append(URLEncoder.encode("TheSnippet", "UTF-8"));
        sb.append("&T=");
        sb.append(URLEncoder.encode(token, "UTF-8"));
        sb.append("&share=");
        sb.append(false);
        sb.append("&url=");
        sb.append(URLEncoder.encode("http://developer.android.com/index.html", "UTF-8"));
        sb.append("&title=");
        sb.append(URLEncoder.encode("TheTitle", "UTF-8"));
        sb.append("&srcTitle=");    
        sb.append(URLEncoder.encode("TheSource", "UTF-8"));
        sb.append("&srcUrl=");    
        sb.append(URLEncoder.encode("www.your_web_site", "UTF-8"));


        //open the connection 
        url = new URL(googleAuthUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        Log.d("GoogleReader", "Auth.c=" + authorization); 
        urlConnection.addRequestProperty("Authorization", "GoogleLogin auth=" + authorization); 
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("Content-Length", Integer.toString(sb.toString().getBytes("UTF-8").length));
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);


        //make a request and retrieve results
        OutputStream outputStream = urlConnection.getOutputStream();
        outputStream.write(sb.toString().getBytes("UTF-8"));
        outputStream.close();
        sb = null;   

        int responseCode = urlConnection.getResponseCode();
        InputStream inputStream;
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inputStream = urlConnection.getInputStream();
            responseString = convertStreamToString(inputStream);
            inputStream.close();
            urlConnection.disconnect(); 
        }else {  
            inputStream = urlConnection.getInputStream();
            responseString = convertStreamToString(inputStream);
            urlConnection.disconnect(); 
            return "error"; 
        }
    } catch (Exception e) {
        return e.getMessage(); 
    }
    return responseString; 

}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

Hope this helps:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try{  
        String auth = getGoogleAuthKey("<your gmail email address>","<your password>");
        String token = getGoogleToken(auth); 
        String result = postToGoogleReader(token, auth); 
        TextView tv = (TextView) findViewById(R.id.textView1); 
        tv.setText(result); 
    }catch(Exception e){
    }

}

protected String getGoogleAuthKey(String _USERNAME, String _PASSWORD) {
    String responseString = ""; 
    URL url;
    try {
        //open the connection 
        url = new URL("https://www.google.com/accounts/ClientLogin");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);  

        //create the body
        StringBuilder sb = new StringBuilder();
        sb.append("accountType="); 
        sb.append("GOOGLE"); 
        sb.append("&Email=");
        sb.append(_USERNAME);
        sb.append("&Passwd=");
        sb.append(_PASSWORD);
        sb.append("&service="); 
        sb.append("reader");
        sb.append("&source=");    
        sb.append("<your app name>"); 

        //make a request and retrieve results
        OutputStream outputStream = urlConnection.getOutputStream();  
        outputStream.write(sb.toString().getBytes("UTF-8"));
        outputStream.close(); 
        sb = null; 

        int responseCode = urlConnection.getResponseCode();
        InputStream inputStream;  
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inputStream = urlConnection.getInputStream();
            responseString = convertStreamToString(inputStream);
            String _AUTHKEY = responseString.substring(responseString.indexOf("Auth="), responseString.length());
            responseString = _AUTHKEY.replace( "Auth=","" );
            inputStream.close();
            urlConnection.disconnect(); 
        }else {  
            urlConnection.disconnect(); 
            return "error"; 
        }
    } catch (Exception e) {
        return e.getMessage();  
    }
    Log.d("GoogleReader", "Auth.a=" + responseString); 
    return responseString.trim();
}

public String getGoogleToken(String authorization) {
    final String googleReaderTokenUrl = "https://www.google.com/reader/api/0/token"; 
    String responseString = ""; 
    URL url;

    try {
        //open the connection 
        url = new URL(googleReaderTokenUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        Log.d("GoogleReader", "Auth.b=" + authorization); 
        urlConnection.addRequestProperty("Authorization", "GoogleLogin auth=" + authorization); 
        urlConnection.setRequestMethod("GET");
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlendcoded");
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);

        try {
             InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
             responseString = convertStreamToString(inputStream);
        }catch(Exception e){
            int responseCode = urlConnection.getResponseCode();
               return Integer.toString(responseCode); 
            //return e.getMessage(); 
        }finally {
             urlConnection.disconnect();
        }
    } catch (Exception e) {
        return e.getMessage(); 
    }
    return responseString; 
}

public String postToGoogleReader(String token, String authorization){
    final String googleAuthUrl = "http://www.google.com/reader/api/0/item/edit"; 
    String responseString = "";  
    URL url;
    try { 

        //create the body
        StringBuilder sb = new StringBuilder();
        sb.append("accountType=");
        sb.append("GOOGLE");
        sb.append("&snippet=");
        sb.append(URLEncoder.encode("TheSnippet", "UTF-8"));
        sb.append("&T=");
        sb.append(URLEncoder.encode(token, "UTF-8"));
        sb.append("&share=");
        sb.append(false);
        sb.append("&url=");
        sb.append(URLEncoder.encode("http://developer.android.com/index.html", "UTF-8"));
        sb.append("&title=");
        sb.append(URLEncoder.encode("TheTitle", "UTF-8"));
        sb.append("&srcTitle=");    
        sb.append(URLEncoder.encode("TheSource", "UTF-8"));
        sb.append("&srcUrl=");    
        sb.append(URLEncoder.encode("www.your_web_site", "UTF-8"));


        //open the connection 
        url = new URL(googleAuthUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        Log.d("GoogleReader", "Auth.c=" + authorization); 
        urlConnection.addRequestProperty("Authorization", "GoogleLogin auth=" + authorization); 
        urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("Content-Length", Integer.toString(sb.toString().getBytes("UTF-8").length));
        urlConnection.setUseCaches(false);
        urlConnection.setDoOutput(true);


        //make a request and retrieve results
        OutputStream outputStream = urlConnection.getOutputStream();
        outputStream.write(sb.toString().getBytes("UTF-8"));
        outputStream.close();
        sb = null;   

        int responseCode = urlConnection.getResponseCode();
        InputStream inputStream;
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inputStream = urlConnection.getInputStream();
            responseString = convertStreamToString(inputStream);
            inputStream.close();
            urlConnection.disconnect(); 
        }else {  
            inputStream = urlConnection.getInputStream();
            responseString = convertStreamToString(inputStream);
            urlConnection.disconnect(); 
            return "error"; 
        }
    } catch (Exception e) {
        return e.getMessage(); 
    }
    return responseString; 

}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文