It's pretty easy to post messages on Twitter, Facebook, MySpace etc. They rely on
a REST server, and basically you make a HttpConnection and send POST or GET messages to an URL, which you can find on API description(e.g. http://api.twitter.com/1/statuses/user_timeline.json)
Twitter allows basic authentication(but in future, will no longer be supported), even though it's better to use oAuth.
On Facebook I'm pretty sure that all you can use it's oAuth
Here's some code for Twitter
public static String viewMyTimeline( String user, String pass)
{
String url ="http://api.twitter.com/1/statuses/user_timeline.json";
String responseM ="";
HttpConnection conn;
try {
//prepare a connection for the twitter API update method
conn = (HttpConnection)Connector.open(url);
conn.setRequestMethod("GET");
byte[] credentialsBytes = (user +":" + pass).getBytes();
String encoded= Base64OutputStream.encodeAsString(credentialsBytes,0,credentialsBytes.length,false,false);
conn.setRequestProperty("Authorization", "Basic " + encoded);
conn.setRequestProperty("Content-Type", "application/xml; charset=utf-8");
System.out.println("respond message=" +conn.getResponseMessage());
System.out.println("-----------------------------" + conn.getResponseCode());
//get the response
InputStream is = conn.openInputStream();
int c;
StringBuffer buffer= new StringBuffer();
while ((c = is.read()) != -1)
{
buffer.append((char)c);
}
System.out.println("user=" + user);
System.out.println("pass=" + pass);
is.close();
responseM = buffer.toString();
System.out.println("response="+responseM);
conn.close();
}
catch (Exception ex)
{ System.out.println("" + ex.getMessage());
}
return responseM;
}
For facebook here's a snapshot to post a message
CEBOOK SET STATUS
public static String sendRequestStatus( String method,String api_key, String uid, String status, String secret ) {
HttpConnection conn = null;
String username =usernameF;
String password =passwordF ;
String message =status;
String response = null;
try {
Hashtable data = new Hashtable();
data.put("method", method);
data.put("v", "1.0");
data.put("call_id", "1");
data.put("api_key",api_key);
data.put("uid",uid);
data.put("status",status);
data.put("format", "JSON");
data.put("sig", getSignature(data,secret));
URLEncodedPostData encoder = new URLEncodedPostData(null, false);
Enumeration keysEnum = data.keys();
String coded="";
while (keysEnum.hasMoreElements()) {
String key = (String)keysEnum.nextElement();
String val = (String)data.get(key);
coded = key+val;
encoder.append(key, val);
}
conn = (HttpConnection)Connector.open("http://api.facebook.com/restserver.php?");
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(encoder.getBytes().length));
OutputStream os = conn.openOutputStream();
os.write(encoder.getBytes());
if (conn.getResponseCode() == HttpConnection.HTTP_OK)
{
StringBuffer buffer = new StringBuffer();
InputStream is = conn.openInputStream();
int c;
while ((c = is.read()) != -1)
{
buffer.append((char)c);
}
is.close();
response = buffer.toString();
}
} catch (Exception e) {
} finally {
if (conn != null) {
try { conn.close(); }
catch (IOException e) {}
}
}
Enough of this, I bored you enough:))
Daftar Harga Molto 2016 Terbaru
Acum 9 ani
Niciun comentariu:
Trimiteți un comentariu