Android und JSON, JSONViewer ein Beispiel
JSON wegen der Einfach- und Kompaktheit ideal für die mobile Kommunikation, enthält das SDK von Android einige Klassen der Referenzimplementierung.

Android :: JSONViewer
Kofiguration
Im Manifest für Android wird die Berechtigung für den Zugriff auf das weltweite Netz (Zeile 7) konfiguriert.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.mgsimon.android.jsonviewer" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".JSONViewerActivity" 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> </manifest> |
JSONViewer/AndroidManifest.xml
Layout
Das Layout enthält ein Eingabefeld (EditText) für die URL, ein Button (ImageButton) zum abrufen der URL und ein mehrzeiliges Textfeld (TextView) zum anzeigen.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayoutUrl" android:layout_width="match_parent"> <EditText android:id="@+id/editTextURL" android:layout_height="wrap_content" android:singleLine="true" android:layout_width="0dp" android:layout_weight="2"> <requestFocus></requestFocus> </EditText> <ImageButton android:id="@+id/imageButtonLoad" android:layout_height="wrap_content" android:src="@android:drawable/ic_media_play" android:layout_width="0dp" android:layout_weight="1"></ImageButton> </LinearLayout> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearLayoutUrl" android:layout_margin="5dip"> <TextView android:id="@+id/textViewContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" /> </ScrollView> </RelativeLayout> |
JSONViewer/res/layout/main.xml
Activity
Die Aktivität JSONViewerActivity erweitert Activity (Zeile 26) und initialisiert notwendige Komponenten in der überschriebene Methode onCreate (Zeile 37). Referenzen zu Elementen des UI werden über findViewById (Zeile 41,43 und 44) gesetzt und dem ImageButton (Zeile 45) wird die aufzurufende Methode für das Event onClick (Zeile 48) überschrieben.
Die Methode onClick – aufgerufen durch Klick auf dem Button – startet mit übergebener URL (Zeile 50) einen Thread (Zeile 51). Die Klasse UrlLoadTask zeigt vor (Zeile 63) dem Thread einen Status an und aktualisiert die Aktivität nach (Zeile 94) dem Thread.
Die Daten werden unabhängig des Threads für die Benutzeroberfläche in der Methode doInBackground (Zeile 69) verarbeitet.
Im Hintergrund herunterladen (Zeile 71-73), JSONObject erzeugen (Zeile 74) und die TextView aktualisieren (Zeile 97).
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | package de.mgsimon.android.jsonviewer; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.HttpResponse; import org.apache.http.ParseException; 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.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; import android.widget.TextView; public class JSONViewerActivity extends Activity { private EditText mEditTextUrl; private ImageButton mImageButtonLoad; private TextView mTextViewContent; private URI mUri; private ProgressDialog mProgressDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mEditTextUrl = (EditText) findViewById(R.id.editTextURL); mEditTextUrl.setText("http://mgsimon.appspot.com/aboutmyrequest"); mTextViewContent = (TextView) findViewById(R.id.textViewContent); mImageButtonLoad = (ImageButton) findViewById(R.id.imageButtonLoad); mImageButtonLoad.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { mUri = new URI(mEditTextUrl.getText().toString()); new UrlLoadTask().execute(mUri); } catch (URISyntaxException e) { e.printStackTrace(); mTextViewContent.setText(e.getMessage()); } } }); } private class UrlLoadTask extends AsyncTask<URI, Void, JSONObject> { @Override protected void onPreExecute() { mProgressDialog = ProgressDialog.show(JSONViewerActivity.this, "", "Loading. Please wait...", true); } @Override protected JSONObject doInBackground(URI... uris) { try { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(uris[0]); HttpResponse response = client.execute(get); JSONObject jso = new JSONObject(EntityUtils.toString(response .getEntity())); return (jso); } catch (ClientProtocolException e) { e.printStackTrace(); mTextViewContent.setText(e.getMessage()); } catch (IOException e) { e.printStackTrace(); mTextViewContent.setText(e.getMessage()); } catch (ParseException e) { e.printStackTrace(); mTextViewContent.setText(e.getMessage()); } catch (JSONException e) { e.printStackTrace(); mTextViewContent.setText(e.getMessage()); } return null; } @Override protected void onPostExecute(JSONObject jso) { try { if (jso != null) mTextViewContent.setText(jso.toString(4)); } catch (JSONException e) { e.printStackTrace(); mTextViewContent.setText(e.getMessage()); } mProgressDialog.dismiss(); } } } |
JSONViewer/src/de.mgsimon.android.jsonviewer/JSONViewerActivity.java
Tags: Android, Java, JSON, SDK