There are three EditText in the application, for user name, password (hiden by password dots) and updated status. Submit to Twitter by pressing of the Submit button. If successful, a Toast with "OK" will be shown, otherwise "ERROR" will be shown.
- Before start; base64 is needed in the application, refer to the article Use base64 in Android to download and include it in the Build Path of the project.
- Modify AndroidManifest.xml, Allows Android applications to open network sockets: "android.permission.INTERNET".
- Modify main.xml to have the input EditText for user name, password and updated status.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="User Name and Password"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="User Name:"
/>
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="What are you doing?"
/>
<EditText
android:id="@+id/whatareyoudoing"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/mySubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Submit"
/>
</LinearLayout>
main.xml can be downloaded here.
- Modify the java as:
package com.exercise.AndroidTwitterClient;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.apache.commons.codec.binary.Base64;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AndroidTwitterClient extends Activity {
EditText MyUsername;
EditText MyPassword;
EditText MyStatus;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button MySubmitButton = (Button)findViewById(R.id.mySubmit);
MySubmitButton.setOnClickListener(MySubmitButtonOnClickListener);
MyUsername = (EditText)findViewById(R.id.username);
MyPassword = (EditText)findViewById(R.id.password);
MyStatus = (EditText)findViewById(R.id.whatareyoudoing);
}
private Button.OnClickListener MySubmitButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (SendTwitter(MyUsername.getText().toString(),
MyPassword.getText().toString(),
MyStatus.getText().toString())){
Toast.makeText(AndroidTwitterClient.this,
"OK", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(AndroidTwitterClient.this,
"ERROR", Toast.LENGTH_SHORT).show();
}
}
};
public static boolean SendTwitter(String twitteruser,
String twitterpass, String msg) {
boolean result = true;
try {
//String twitteruser = "android_er";
//String twitterpass = "pass_word";
URLConnection connection = null;
String credentials = twitteruser + ":" + twitterpass;
String encodecredentials =
new String(Base64.encodeBase64(credentials.getBytes()));
URL url = new URL("http://twitter.com/statuses/update.xml");
String encodedData = URLEncoder.encode(msg, "UTF-8");
connection = url.openConnection();
connection.setRequestProperty( "Authorization",
"Basic " + encodecredentials);
connection.setDoOutput(true);
OutputStreamWriter out = new
OutputStreamWriter(connection.getOutputStream());
out.write("status=" + encodedData);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
while (in.readLine() != null) {
}
in.close();
} catch (Exception e) {
result = false;
}
return result;
}
}
AndroidTwitterClient.java can be downloaded here.
7 comments:
Is this codes available now? Can you update still? There is some Exception - FileNotFound (the status xml file) on getInputStream.
Can you have another notes?
yes I have the same doubt as Steven.
file not found exception!
Hello Sir..I am trying to do this but the problem is that i am continuously getting error...when i insert the http://twitter.com/statuses/update.xml so can you please tell me how can i rectify this...
I think this method have been outdated!
okie....thank you so much sir but than can you please help me out with the modified method...
Dude / Dudette, you should really put a BIG Header in this post.. this NO longer works (as from yyyy-mm-dd).. let's continue the search for the current Twitter API stuff ;)
thank you @Kees , you saved some important minutes of my life :)
Post a Comment