Sunday 20 October 2013

package com.niticentral.Interface;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class Http {
    JSONObject jo_search, jo_category, jo_category_story, jo_get_recent_post,
            jo_story_detail, jo_res_comment, jo_res_newsletter,
            jo_get_author_post, jo_get_author_list, jo_res_about_us,
            facebook_wallpost,jo_short_url;
    JSONArray jr_comments_list;
    String apikey = "26fcfd6c27628f6947fe27e4a302f4ca";
    String sort_api_key = "R_83b6276d7ae7c97ec01755df963d4650";
        String sort_api_name= "nitidigital";
           
    String record_count = "15";
    //<string name="url"></string>
    //String sort_api = "http://api.bitly.com/v3/shorten/?login=nitidigital&apiKey=R_83b6276d7ae7c97ec01755df963d4650&longUrl=http://www.niticentral.com/2013/05/20/we-cant-afford-to-be-junked-79694.html";
    public JSONObject get_sort_url (String long_url){
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> responsehandler = new BasicResponseHandler();
            HttpGet getmethod = new HttpGet("http://api.bitly.com/v3/shorten/?login="+sort_api_name+"&apiKey="+sort_api_key+"&longUrl="+long_url+"");
            String response = httpclient.execute(getmethod, responsehandler);
            jo_short_url = new JSONObject(response);
            //
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jo_short_url;
       
    }
    public JSONObject search1(String page, String key) {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> responsehandler = new BasicResponseHandler();
            HttpGet getmethod = new HttpGet(Constants.base_url
                    + "get_search_results/?dev=1&search="+key+"&count="
                    + record_count + "&page=" + page + "&apikey=" + apikey + "");
            String response = httpclient.execute(getmethod, responsehandler);
            jo_search = new JSONObject(response);
            //
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jo_search;

    }
    public JSONObject search(String page, String key) {
        // http://beta.niticentral.com/api/respond/submit_comment/?dev=1&post_id=15782&name=rami&email=rami@gmail.com&content=rrrrrrrr&apikey=26fcfd6c27628f6947fe27e4a302f4ca
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            ResponseHandler<String> resonseHandler = new BasicResponseHandler();
   
            HttpPost getmethod = new HttpPost(Constants.base_url
                    + "get_search_results/?dev=1");
            // http://www.niticentral.com/api/respond/submit_comment/?dev=1
           
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("search", key));
            nameValuePairs.add(new BasicNameValuePair("count", record_count));
            nameValuePairs.add(new BasicNameValuePair("page", page));
           
            nameValuePairs.add(new BasicNameValuePair("apikey", apikey));

            getmethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            String response = httpClient.execute(getmethod, resonseHandler);
            //Constants.appendLog("res"+response);
            jo_search = new JSONObject(response);
           

        } catch (Exception e) {
            e.printStackTrace();
        }
        return jo_search;

    }

    public JSONObject get_category() {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> responsehandler = new BasicResponseHandler();

            HttpGet getmethod = new HttpGet(Constants.base_url
                    + "get_category_index/?dev=1&catid=214&apikey=" + apikey
                    + "");
            String response = httpclient.execute(getmethod, responsehandler);
            jo_category = new JSONObject(response);
            // //Log.e("","jr_res_category"+jo_category);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jo_category;

    }

    public JSONObject get_category_story(String page, String cat_id) {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> responsehandler = new BasicResponseHandler();

            HttpGet getmethod = new HttpGet(Constants.base_url
                    + "get_category_posts/?dev=1&id=" + cat_id + "&count="
                    + record_count + "&page=" + page + "&apikey=" + apikey + "");
            String response = httpclient.execute(getmethod, responsehandler);
            jo_category_story = new JSONObject(response);
            // //Log.e("","jr_res_category"+jo_category_story);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jo_category_story;

    }

    public JSONObject get_story_detail(String story_id,String width) {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> responsehandler = new BasicResponseHandler();
           
            HttpGet getmethod = new HttpGet(Constants.base_url
                    + "get_post/?dev=1&id=" + story_id + "&width="+width+"&apikey=" + apikey
                    + "");
            String response = httpclient.execute(getmethod, responsehandler);
            jo_story_detail = new JSONObject(response);
            // //Log.e("","jo_story_detail"+jo_story_detail);
        //    Constants.appendLog("res jo_story_detail"+jo_story_detail);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jo_story_detail;

    }

    public JSONObject get_author_list(String page) {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> responsehandler = new BasicResponseHandler();

            HttpGet getmethod = new HttpGet(Constants.base_url
                    + "get_author_index_paging/?dev=1&id=23268&apikey="
                    + apikey + "&count="+record_count+"&page=" + page + "");
            String response = httpclient.execute(getmethod, responsehandler);
            jo_get_author_list = new JSONObject(response);
            // //Log.e("","jo_get_author_list"+jo_get_author_list);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jo_get_author_list;

    }

    public JSONObject get_author_post(String author_id, String page) {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> responsehandler = new BasicResponseHandler();

            HttpGet getmethod = new HttpGet(Constants.base_url
                    + "get_author_posts/?dev=1&id=" + author_id + "&count="
                    + record_count + "&page=" + page + "&apikey=" + apikey + "");
            String response = httpclient.execute(getmethod, responsehandler);
            jo_get_author_post = new JSONObject(response);
            // //Log.e("","jo_story_detail"+jo_story_detail);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jo_get_author_post;

    }

    public JSONArray get_comment_list(String story_id) {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> responsehandler = new BasicResponseHandler();

            HttpGet getmethod = new HttpGet(Constants.base_url
                    + "get_posts_comments/?dev=1&post_id=" + story_id
                    + "&apikey=" + apikey + "");
            String response = httpclient.execute(getmethod, responsehandler);
            jr_comments_list = new JSONArray(response);
            //Log.e("", "jo_story_detail" + jr_comments_list);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jr_comments_list;

    }

    public JSONObject send_comment(String post_id, String name, String email,
            String content) {
        // http://beta.niticentral.com/api/respond/submit_comment/?dev=1&post_id=15782&name=rami&email=rami@gmail.com&content=rrrrrrrr&apikey=26fcfd6c27628f6947fe27e4a302f4ca
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            ResponseHandler<String> resonseHandler = new BasicResponseHandler();
   
            HttpPost getmethod = new HttpPost(Constants.base_url
                    + "respond/submit_comment/?dev=1");
            // http://www.niticentral.com/api/respond/submit_comment/?dev=1
           
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
            nameValuePairs.add(new BasicNameValuePair("post_id", post_id));
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("email", email));
            nameValuePairs.add(new BasicNameValuePair("content", content));
            nameValuePairs.add(new BasicNameValuePair("apikey", apikey));

            getmethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            String response = httpClient.execute(getmethod, resonseHandler);
            //Constants.appendLog("res"+response);
            jo_res_comment = new JSONObject(response);
           

        } catch (Exception e) {
            e.printStackTrace();
        }
        return jo_res_comment;

    }

    public JSONObject news_letter(String action, String email_id) {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> responsehandler = new BasicResponseHandler();

            HttpGet getmethod = new HttpGet(Constants.base_url + "respond/"
                    + action + "/?dev=1&email=" + email_id + "&apikey="
                    + apikey + "");
            String response = httpclient.execute(getmethod, responsehandler);
            jo_res_newsletter = new JSONObject(response);
            //Log.e("", "Res" + response);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jo_res_newsletter;

    }

    public JSONObject cmspage(String key) {
        try {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            ResponseHandler<String> resonseHandler = new BasicResponseHandler();
            HttpGet get_method = new HttpGet(Constants.base_url
                    + "get_page/?dev=1&slug=" + key
                    + "&apikey=26fcfd6c27628f6947fe27e4a302f4ca");
            String response = httpclient.execute(get_method, resonseHandler);
            jo_res_about_us = new JSONObject(response);
            // //Log.e("","jo_res_author_post"+jo_res_author_post);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jo_res_about_us;

    }

}
------------------------------------------------------------------------------------------------------
try {
                Http get_category = new Http();
                jo_res_category = get_category.get_category();
                jr_res_category = jo_res_category.getJSONArray("categories");
                for (int i = 0; i < jr_res_category.length(); i++) {
                    jo_category_details = jr_res_category.getJSONObject(i);
                    category_id.add(jo_category_details.getString("id"));
                    category_name.add(jo_category_details.getString("name"));
                }
                settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                int j;
                editor.putInt("size", category_id.size());
                for (j = 0; j < category_id.size(); j++) {
                    editor.putString("catid" + j, category_id.get(j));
                    editor.putString("catname" + j, category_name.get(j));
                }
                // Commit the edits!
                editor.commit();
                flag_first = false;

            } catch (Exception e) {
                //FlurryAgent.onError("uncaught", "crash", "exception");
                FlurryAgent.onError("Crash", "Exception in Home Page in android PhoneModel "
                        + PhoneModel + " of version " + AndroidVersion,
                        e.getMessage());

            }

No comments:

Post a Comment