2015-02-10 54 views
2

我使用它的API實現了reddit集成。下面是我的登錄代碼到reddit的:在Android的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 & D,但沒有太多的細節或例子可用於reddit集成。

那麼,有助於解決這個錯誤。

+0

有費率限制。 http://www.reddit.com/r/webdev/comments/1z8tqk/reddit_api_rate_limit_question/請參閱https://github.com/reddit/reddit/wiki/API#rules – Kenneth 2015-02-10 11:18:52

+0

我應該在我的代碼中更改哪些內容? – Riser 2015-02-10 12:00:03

回答

1

從API文檔:

Make no more than thirty requests per minute 

你將會在這個數字,因此您的申請將被拒絕。

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

+0

我應該在我的代碼中更改哪些內容? – Riser 2015-02-10 11:55:51

+0

發生這種情況時,向用戶顯示一條消息以解釋他必須等待。沒有解決方法。 – 2015-02-10 12:01:30

+0

你也可以看到這個問題。 http://stackoverflow.com/questions/28431599/submit-post-using-reddit-api-in-android – Riser 2015-02-10 12:24:41

2

除了安東尼奧MG的回答,您應該設置一個自定義的用戶代理,每reddit API rules

「很多默認的用戶代理(如‘巨蟒/ 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

+0

你可以給一個OAuth2登錄的例子在Android? – Riser 2015-02-11 05:30:11

+0

我會推薦代碼流,並請求刷新令牌。更多信息在OAuth2流reddit:https://github.com/reddit/reddit/wiki/OAuth2 – kemitche 2015-02-11 19:29:43

+0

我已經看到這從最近3天,但不知道如何登錄並提交一個鏈接使用。 – Riser 2015-02-12 04:54:39