Call Plurk API by HttpClient 4

| 2 TrackBacks

之前的噗浪(Plurk)期貨即時訊息文章中,說道IM發噗會有掉噗情況,目前改用Plurk Api後,情況有改善。程式如下:

import java.io.IOException;

import java.net.URLEncoder;


import org.apache.http.HttpHost;

import org.apache.http.HttpResponse;

import org.apache.http.HttpStatus;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.CookieStore;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.conn.params.ConnRoutePNames;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;


public class PlurkApi {


private static final String API_KEY = "KEYSTRING";

static boolean use_proxy = false;

static String PROXY_NAME = "proxy.hinet.net";

static int PROXY_PORT = 80;

static CookieStore cookiestore;

static final String username= "youraccount";

static final String password= "yourpassword";

private volatile static PlurkApi plurk;

private PlurkApi(){}

public static PlurkApi getInstance(){

if (plurk == null) {

synchronized (PlurkApi.class){

if (plurk == null) {

plurk = new PlurkApi();

}

}

}

return plurk;

}


    public static String getApiUri(String uri) {

        return "http://www.plurk.com/API" + uri;

    }


    public final static void main(String[] args) {

     PlurkApi p = PlurkApi.getInstance();

     p.plurkAdd("API測試111111!");

     p.logout();

    }

    

    public String login() {

     DefaultHttpClient httpclient = new DefaultHttpClient();

        if (use_proxy) {

HttpHost proxy = new HttpHost(PROXY_NAME, PROXY_PORT);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,

proxy);

}

        

        HttpGet httpget = new HttpGet(getApiUri("/Users/login?"+

                            "api_key=" + API_KEY + "&" +

                            "username=" + username + "&" +

                            "password=" + password

                          ));

        HttpResponse response = null;

String responseString = null;

try {

response = httpclient.execute(httpget);

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

cookiestore = httpclient.getCookieStore();

responseString = EntityUtils.toString(response.getEntity());

// 如果回傳是 200 OK 的話才輸出

// System.out.println(responseString);

//

} else {

System.out.println(response.getStatusLine());

responseString = EntityUtils.toString(response.getEntity());

System.out.println(responseString);

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

httpclient.getConnectionManager().shutdown();

return responseString;

    }

    

    public String plurkAdd(String url) {

     PlurkApi p = PlurkApi.getInstance();

     if (cookiestore == null)

     p.login();

     DefaultHttpClient httpclient = new DefaultHttpClient();

        if (use_proxy) {

HttpHost proxy = new HttpHost(PROXY_NAME, PROXY_PORT);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,

proxy);

}

        httpclient.setCookieStore(cookiestore);

       

        HttpResponse response = null;

String responseString = null;

try {

String content = URLEncoder.encode(url, "UTF-8");

HttpGet httpget = new HttpGet(getApiUri("/Timeline/plurkAdd?"+

               "api_key=" + API_KEY + "&" +

               "content=" + content + "&" +

               "qualifier=" + "says" + "&" +

               "lang=tr_ch"));

response = httpclient.execute(httpget);

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

cookiestore = httpclient.getCookieStore();

responseString = EntityUtils.toString(response.getEntity());

// 如果回傳是 200 OK 的話才輸出

// System.out.println(responseString);

//

} else {

System.out.println(response.getStatusLine());

responseString = EntityUtils.toString(response.getEntity());

System.out.println(responseString);

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

httpclient.getConnectionManager().shutdown();

return responseString;

    }


    public String logout() {

     PlurkApi p = PlurkApi.getInstance();

     if (cookiestore == null)

     p.login();

     DefaultHttpClient httpclient = new DefaultHttpClient();

        if (use_proxy) {

HttpHost proxy = new HttpHost(PROXY_NAME, PROXY_PORT);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,

proxy);

}

        httpclient.setCookieStore(cookiestore);

        HttpResponse response = null;

String responseString = null;

try {

HttpGet httpget = new HttpGet(getApiUri("/Users/logout?"+

                     "api_key=" + API_KEY));

response = httpclient.execute(httpget);

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

cookiestore = httpclient.getCookieStore();

responseString = EntityUtils.toString(response.getEntity());

// 如果回傳是 200 OK 的話才輸出

// System.out.println(responseString);

//

} else {

System.out.println(response.getStatusLine());

responseString = EntityUtils.toString(response.getEntity());

System.out.println(responseString);

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

httpclient.getConnectionManager().shutdown();

return responseString;

    }

}

2 TrackBacks

TrackBack URL: http://server.everfine.com.tw/blog/mt-tb.cgi/305

從自由軟體鑄造場得知Microsoft 打開 Messenger 大門 開始支援 XMPP,因此目前主流即時訊息都支援XMPP,包括Facebook chat。 既然如此,當然找看看熟悉的Smack套件是否也有MSN的sample code。果然Github上有MS放出的LiveSDK,便有JAVA跟Android的程式碼。有空再把拿token那程式改成用httpclient。 JAVA程式碼如下: package messenger;import java.net.URL;import java.... Read More

kick ass torrents proxy from kick ass torrents proxy on January 26, 2014 4:44 AM

Call Plurk API by HttpClient 4 - Philipz學習日誌 Read More

January 2014

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Archives

About this Entry

This page contains a single entry by philipz published on November 24, 2010 10:13 AM.

如何遠端呼叫需特定權限的Google App Engine service was the previous entry in this blog.

Gson Exception: No-args constructor for class XXX does not exit. is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.