/ Published in: Java
It is application base on json data retriving from a remote URL
Expand |
Embed | Plain Text
//JsonActivity.java package com.v3; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreProtocolPNames; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONStringer; import android.app.Activity; import android.os.Bundle; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class JsonActivity extends Activity { Button button,buttonClear; TextView textView; String page; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView) findViewById(R.id.txtView); { { examineJSONFile(); } }); { { textView.setText(""); } }); } { //This method for HttpConnection try { HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android"); HttpGet request = new HttpGet(); request.setHeader("Content-Type", "text/plain; charset=utf-8"); HttpResponse response = client.execute(request); while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line + NL); } bufferedReader.close(); page = stringBuffer.toString(); return page; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } { Log.d("BBB", e.toString()); } } } } void examineJSONFile() { try { page = executeHttpGet(url); Log.i("hello", str.toString()); JSONObject object=new JSONObject(page); str=str+object.names().toString(); for (int i=0;i<names.length;i++) { Value=Value+names[i] +":"+object.get(names[i])+"\n"; } /* String sub=str.substring(1, str.length()-1); String[] names=sub.split(","); String[] keyValue=new String[names.length]; for (int i=0;i<names.length;i++) { names[i]=names[i].replace("\"",""); Value=Value+names[i] +":"+object.get(names[i])+"\n"; //keyValue[i]=names[i] +":"+object.get(names[i])+"\n"; System.out.println(names[i]); }*/ textView.setText(Value); /*for(int i=0;i<keyValue.length;i++){ textView.setText(keyValue[i]); }*/ Log.i("hello", str.toString()); } { textView.setText("Error w/file: " + je.getMessage()); } } } //main.xml //================== <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#acd654" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:text="Fetch_Data" android:id="@+id/btnFetch" android:layout_width="match_parent" android:layout_height="wrap_content"></Button> <Button android:text="Clear_Screen" android:id="@+id/btnClear" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <TextView android:text="" android:gravity="center" android:textColor="#000a44" android:id="@+id/txtView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> //android menifest.xml //========================= ?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.v3" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".JsonActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest>
You need to login to post a comment.
