Search code examples
androidreddit

Reddit integration in android


i implemented reddit integration using API of it. below is my code for login into 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();
        }

}

using above method, its only works sometimes and mostly get response like below:

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>

i dont know how to deal with this type of problem. i already did R&D on that but there are not much details or examples available for reddit integration.

So, help to solve this bug.


Solution

  • From the API Documentation:

    Make no more than thirty requests per minute
    

    You are going over that number and therefore your request are rejected.

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