在Android中的Reddit集成

时间:2015-02-10 11:15:27

标签: android reddit

我使用它的API实现了reddit集成。下面是我登录reddit的代码:

private void LoginMethod(){

        String jsonString = "";
        DefaultHttpClient httpclient = new DefaultHttpClient();

        final ArrayList<NameValuePair> fields = new ArrayList<NameValuePair>(3);
        fields.add(new BasicNameValuePair("user", "test"));//will ask for a user to enter the password later
        fields.add(new BasicNameValuePair("passwd", "test11"));
        fields.add(new BasicNameValuePair("api_type", "json"));

        final HttpPost request = new HttpPost("https://ssl.reddit.com/api/login");

        try {
            request.setEntity(new UrlEncodedFormEntity(fields, HTTP.UTF_8));

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            jsonString = EntityUtils.toString(entity);

            System.out.println("response from redit = " + jsonString);

            JSONObject jObject = new JSONObject(jsonString);
            modhash = jObject.getJSONObject("json").getJSONObject("data").getString("modhash");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

}

使用上述方法,它只能有效地工作,并且大多数得到如下的响应:

02-10 16:36:57.008: I/System.out(26970): <!doctype html>
02-10 16:36:57.008: I/System.out(26970): <html>
02-10 16:36:57.008: I/System.out(26970):   <head>
02-10 16:36:57.008: I/System.out(26970):     <title>Too Many Requests</title>
02-10 16:36:57.008: I/System.out(26970):     <style>
02-10 16:36:57.008: I/System.out(26970):       body {
02-10 16:36:57.008: I/System.out(26970):           font: small verdana, arial, helvetica, sans-serif;
02-10 16:36:57.008: I/System.out(26970):           width: 600px;
02-10 16:36:57.008: I/System.out(26970):           margin: 0 auto;
02-10 16:36:57.008: I/System.out(26970):       }
02-10 16:36:57.008: I/System.out(26970):       h1 {
02-10 16:36:57.008: I/System.out(26970):           height: 40px;
02-10 16:36:57.008: I/System.out(26970):           background: transparent url(//www.redditstatic.com/reddit.com.header.png) no-repeat scroll top right;
02-10 16:36:57.008: I/System.out(26970):       }
02-10 16:36:57.008: I/System.out(26970):     </style>
02-10 16:36:57.008: I/System.out(26970):   </head>
02-10 16:36:57.008: I/System.out(26970):   <body>
02-10 16:36:57.008: I/System.out(26970):     <h1>whoa there, pardner!</h1>
02-10 16:36:57.008: I/System.out(26970):     
02-10 16:36:57.008: I/System.out(26970): <p>we're sorry, but you appear to be a bot and we've seen too many requests
02-10 16:36:57.008: I/System.out(26970): from you lately. we enforce a hard speed limit on requests that appear to come
02-10 16:36:57.008: I/System.out(26970): from bots to prevent abuse.</p>
02-10 16:36:57.008: I/System.out(26970): <p>if you are not a bot but are spoofing one via your browser's user agent
02-10 16:36:57.008: I/System.out(26970): string: please change your user agent string to avoid seeing this message
02-10 16:36:57.008: I/System.out(26970): again.</p>
02-10 16:36:57.008: I/System.out(26970): <p>please wait 3 second(s) and try again.</p>
02-10 16:36:57.008: I/System.out(26970):     <p>as a reminder to developers, we recommend that clients make no
02-10 16:36:57.008: I/System.out(26970):     more than <a href="http://github.com/reddit/reddit/wiki/API">one
02-10 16:36:57.008: I/System.out(26970):     request every two seconds</a> to avoid seeing this message.</p>
02-10 16:36:57.008: I/System.out(26970):   </body>
02-10 16:36:57.008: I/System.out(26970): </html>

我不知道如何处理这类问题。我已经做过R&amp; D,但没有太多的细节或例子可用于reddit集成。

所以,帮助解决这个错误。

2 个答案:

答案 0 :(得分:2)

除了Antonio MG的回答,你应该根据reddit API rules设置自定义用户代理:

  

“许多默认的用户代理(如”Python / urllib“或”Java“)受到极大限制,以鼓励使用唯一的描述性用户代理字符串。”

至少应包括reddit员工的联系信息,以便在必要时与他们取得联系。根据要求,它应该是以下形式:

<platform>:<app ID>:<version string> (by /u/<reddit username>)

例如

android:com.example.myredditapp:v1.2.3 (by /u/kemitche)

最后,您应该强烈考虑使用OAuth 2来访问reddit的API,而不是直接登录; "cookie" authentication is scheduled for deprecation

答案 1 :(得分:1)

来自API文档:

Make no more than thirty requests per minute

您正在查看该号码,因此您的请求被拒绝。

https://github.com/reddit/reddit/wiki/API#rules