Auffüllen von JSON von diesem link zur android-Listview
Ich versuche, die Anzeige der Elemente in listview aus JSON::
JsonURL- https://dl.dropboxusercontent.com/s/rhk01nqlyj5gixl/jsonparsing.txt?token_hash=AAHpfauVxJaC9Rkx_5abNtJnPFG04os7TZky1AhOuC5jEw
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listViewID"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
</ListView>
</LinearLayout>
JSONParser.java
package com.example.testjson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONArray jObj = null;
static String json = "";
//constructor
public JSONParser() {
}
public JSONArray getJSONFromUrl(String url) {
//Making HTTP request
try {
//defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
//try parse the string to a JSON object
try {
jObj = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
//return JSON String
return jObj;
}
}
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dip"
android:maxLines="1"
android:text="Name"
android:textColor="@android:color/black"
android:textSize="14dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/tvcity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvname"
android:layout_marginLeft="5dip"
android:layout_marginTop="3dip"
android:maxLines="1"
android:text="City"
android:textColor="@android:color/black"
android:textSize="14dp" >
</TextView>
<TextView
android:id="@+id/tvbdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvcity"
android:layout_marginLeft="5dip"
android:layout_marginTop="3dip"
android:maxLines="1"
android:text="Birthdate"
android:textColor="@android:color/black"
android:textSize="14dp" >
</TextView>
<TextView
android:id="@+id/tvgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dip"
android:maxLines="1"
android:text="Gender"
android:textColor="@android:color/black"
android:textSize="14dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/tvage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tvgender"
android:layout_marginRight="15dip"
android:layout_marginTop="3dip"
android:maxLines="1"
android:text="Age"
android:textColor="@android:color/black"
android:textSize="14dp" >
</TextView>
</RelativeLayout>
MainActivity.java
package com.example.testjson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.text.StaticLayout;
import android.view.Menu;
public class MainActivity extends Activity {
private static String url="https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create a JSON parser Instance ----- Used JSON parser from Android
JSONParser jParser=new JSONParser();
//Getting JSON string from URL ------ Used JSON Array from Android
JSONArray json=jParser.getJSONFromUrl(url);
try {
for(int i=0;i<json.length();i++)
{
JSONObject c=json.getJSONObject(i);//Used JSON Object from Android
//Storing each Json in a string variable
int AGE=c.getInt("age");
String NAME=c.getString("name");
String CITY=c.getString("city");
String GENDER=c.getString("Gender");
String BIRTHDATE=c.getString("birthdate");
}
} catch (JSONException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- Ich versuche, füllen Sie die Daten aus der JSON -, listView -, einige
Mehrdeutigkeit bei der BesetzungMainActivity.java
- was die Sammlung für diese Funktionalität ?
- Irgendwelche Ideen, Das sieht eine einfache Aufgabe, aber für einen Anfänger wie mich seine
was eine Menge von kämpfen zu lernen
Dank ,
Gute Frage ! .... Auch ich bin auf der Suche nach so etwas ..... und gute Erklärung über das problem !... Wird auf Antworten zu warten !
InformationsquelleAutor | 2013-08-01
Schreibe einen Kommentar Antworten abbrechen
Du musst angemeldet sein, um einen Kommentar abzugeben.
Fortsetzung Ihrer Klasse:
integrierter Stromversorgung:
Objekt holding Daten:
xml für listview-Element (speichern unter
itemlistrow
name):Wiederverwendet Elemente aus https://stackoverflow.com/a/8166802/1276374
Einfacheres Beispiel:
Dank http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html
Mocialov Boris ..... Ich habe versucht, Ihre Lösung mit Ihrer ersten Lösung ... ich bin immer der Fehler, wie unten in meiner LogCat .................................. 08-01 14:38:15.625: E/Buffer-Fehler(710): Error converting-Ergebnis java.lang.NullPointerException 08-01 14:38:15.634: E/JSON-Parser(710): Fehler beim Parsen der Daten org.json.JSONException: End of input at character 0 of 08-01 14:38:15.644: D/AndroidRuntime(710): das Herunterfahren der VM .........................auch einige Fehler mehr !
scheint, wie Sie eine Ausnahme von Ihrer JSON-parser
geändert einige Dinge in meiner Antwort
Item
Objekt ersetztWhateverObject
, das gleiche zu tunändern
ArrayList
zuList
imListAdapter
- Antwort bearbeitetInformationsquelleAutor Boris Mocialov
Erstellen Sie eine Klasse, sagt der Student, enthalten einen Datensatz, erstellen Sie ein array von Studenten aus den JSON, und verwenden Sie dann, dass mit ArrayAdapter füllen Sie Ihre ListView.
InformationsquelleAutor Ben Williams
Verwendung von ArrayList besser für Sie.Finden Sie unter den folgenden Codeausschnitt,
Deklarieren Sie die folgenden auf der Oberseite,
Und die folgenden innerhalb des try-Blocks nach dem abrufen der
Name
Und Sie können leicht Holen sich die Werte aus der arraylist, die durch den index-Wert wie folgt,
InformationsquelleAutor Spring Breaker
Ich würde GSON-Bibliothek für seine Einfachheit. In diesem Fall haben Sie die folgenden:
Ein POJO-Schüler-Klasse für Daten-mapping
Analysieren die Daten nur tun,
Und von hier aus fügen Sie einfach die Daten an den adapter, und das ist es.
InformationsquelleAutor Axxiss