Progress dialogue Added
Don't forget to add Internet Permission.
XML
Same as previous article
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tvJson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tweets:" />
</LinearLayout>
</ScrollView>
</LinearLayout>
java file (MainActivity.java)
package com.example.jsontwiter;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
String completeData = "";
TextView tv;
// jsonArray used in place of jsonobject
JSONArray jsoon;
HttpClient client;
final static String URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tvJson);
// text is the key reference
new Read().execute("text");
}
// JSONarray instead of object
public JSONArray lastTweet(String username) throws ClientProtocolException , IOException , JSONException{
client = new DefaultHttpClient();
StringBuilder urll = new StringBuilder(URL);
// .append --add whatever is passed in the above STRING name i.e username
urll.append(username);
HttpGet get = new HttpGet(urll.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
// 200 is sucesful
if (status == 200) {
HttpEntity entity = r.getEntity();
String data = EntityUtils.toString(entity);
JSONArray timeline = new JSONArray(data);
// There is no json data here
return timeline;
}
else
{
Toast.makeText(MainActivity.this, "Error",
Toast.LENGTH_SHORT).show();
return null;
}
}
// Use Array list for result paramater
public class Read extends AsyncTask<String, Integer,
ArrayList<String>>
{
// SHOW PROGESS DIALOG
ProgressDialog pd = new ProgressDialog(MainActivity.this);
protected void onPreExecute(){
pd.setMessage("Loading...");
pd.show();
}
@Override
protected ArrayList<String> doInBackground(String... params) {
// TODO Auto-generated method stub
ArrayList<String> al = new ArrayList<String>();
try {
jsoon = lastTweet("mehviish");
int count = jsoon.length();
JSONObject jobj = new JSONObject();
for (int i = 0; i < count; i++) {
jobj = jsoon.getJSONObject(i);
al.add(jobj.getString("text").toString());
}
return al ;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<String> al) {
// TODO Auto-generated method stub
pd.dismiss();
for (String string : al) {
completeData += string + System.getProperty("line.separator")
+ System.getProperty("line.separator");
}
tv.setText("Tweets : " + System.getProperty("line.separator")
+ completeData);
}
}
}
No comments:
Post a Comment