我試圖解析一個JSON對象的幫助下(本軟件包http://www.youtube.com/watch?v=0Lr37suTPpg)的幫助。我一步一步地按照說明操作,並設法得到了工作的例子。但是,當我修改示例以使用我自己的json對象時,雖然沒有錯誤,但模擬器只顯示空白屏幕。Android執行完美,但不顯示任何東西
請記住,我的json對象是匿名的(請參閱下面的鏈接)不像這個例子。我已經在代碼中對這個問題做了一些修改,但是我不確定它們。無論如何,我一直無法找出問題的原因,所以我想請求你的幫助。下面是我的代碼(3個java文件和2個xml),下面是這個例子的代碼(顯然也是3個java和2個xml)。
Sights.java(又名MainActivity.java /同樣在這裏是JSON對象的鏈接)
package com.example.thodoras.jsontutorialtest;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
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 java.io.IOException;
import java.util.ArrayList;
public class Sights extends Activity {
ListView list;
SightsAdapter adapter;
ArrayList<HelperSights> sightsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sights);
list = (ListView) findViewById(R.id.list);
sightsList = new ArrayList<HelperSights>();
new SightsAsyncTask().execute("http://www.escapeguide.gr/en/rest/views/mobile_listings.json?display_id=sights_service");
}
public class SightsAsyncTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONArray array = new JSONArray(data);
for (int i = 0; i < array.length(); i++) {
HelperSights sights = new HelperSights();
JSONObject obj = array.getJSONObject(i);
sights.setImage(obj.getString("cover image"));
sights.setTitle(obj.getString("title"));
sights.setLocation(obj.getString("location"));
sightsList.add(sights);
}
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result == false) {
//something
} else {
SightsAdapter adapter = new SightsAdapter(getApplicationContext(), R.layout.row, sightsList);
list.setAdapter(adapter);
}
}
}
}
package com.example.thodoras.jsontutorialtest;
HelperSights.java(包含吸氣劑設置器)
public class HelperSights {
private String image;
private String title;
private String location;
public HelperSights(){
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
SightsAdapter.java
p ackage com.example.thodoras.jsontutorialtest;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.InputStream;
import java.util.ArrayList;
public class SightsAdapter extends ArrayAdapter<HelperSights> {
ArrayList<HelperSights> ArrayListSights;
int Resource;
Context context;
LayoutInflater vi;
public SightsAdapter(Context context, int resource, ArrayList<HelperSights> objects) {
super(context, resource, objects);
ArrayListSights = objects;
Resource = resource;
this.context = context;
vi = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = vi.inflate(Resource, null);
holder = new ViewHolder();
holder.sImage = (ImageView)convertView.findViewById(R.id.sImage);
holder.sTitle = (TextView)convertView.findViewById(R.id.sTitle);
holder.sLocation = (TextView)convertView.findViewById(R.id.sLocation);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
new DownloadImageTask(holder.sImage)
.execute(ArrayListSights.get(position).getImage());
holder.sTitle.setText(ArrayListSights.get(position).getTitle());
holder.sLocation.setText(ArrayListSights.get(position).getLocation());
return convertView;
}
static class ViewHolder{
public ImageView sImage;
public TextView sTitle;
public TextView sLocation;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
activity_sights.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="#F1F1F1"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/row" >
</ListView>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="@+id/LinearLayout1"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/sImage"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sTitle"
android:text="Yo
"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sLocation"
android:text="Man"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
下面是成功的例子,可以幫助你比較的文件。
MainActivity.java(包含JSON對象的鏈接)
package com.example.thodoras.jsontutorail;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
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 java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends Activity {
ListView list;
ActorsAdapter adapter;
ArrayList<Actors> actorsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
actorsList = new ArrayList<Actors>();
new ActorAsyncTask()
.execute("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors");
}
public class ActorAsyncTask extends AsyncTask<String, Void, Boolean>{
@Override
protected Boolean doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jObj = new JSONObject(data);
JSONArray jArray = jObj.getJSONArray("actors");
for (int i = 0; i < jArray.length(); i++) {
Actors actor = new Actors();
JSONObject jRealObject = jArray.getJSONObject(i);
actor.setName(jRealObject.getString("name"));
actor.setDescription(jRealObject.getString("description"));
actor.setDob(jRealObject.getString("dob"));
actor.setCountry(jRealObject.getString("country"));
actor.setHeight(jRealObject.getString("height"));
actor.setSpouse(jRealObject.getString("spouse"));
actor.setChildren(jRealObject.getString("children"));
actor.setImage(jRealObject.getString("image"));
actorsList.add(actor);
}
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result == false){
finish();
} else {
ActorsAdapter adapter = new ActorsAdapter
(getApplicationContext(),R.layout.row, actorsList);
list.setAdapter(adapter);
}
}
}
}
Actors.java(類似於HelperSights.java)
package com.example.thodoras.jsontutorail;
public class Actors {
private String name;
private String description;
private String dob;
private String country;
private String height;
private String spouse;
private String children;
private String image;
public Actors(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getSpouse() {
return spouse;
}
public void setSpouse(String spouse) {
this.spouse = spouse;
}
public String getChildren() {
return children;
}
public void setChildren(String children) {
this.children = children;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
ActorsAdapter.java
package com.example.thodoras.jsontutorail;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.InputStream;
import java.util.ArrayList;
public class ActorsAdapter extends ArrayAdapter<Actors> {
ArrayList<Actors> ArrayListActors;
int Resource;
Context context;
LayoutInflater vi;
public ActorsAdapter(Context context, int resource, ArrayList<Actors> objects) {
super(context, resource, objects);
ArrayListActors = objects;
Resource = resource;
this.context = context;
vi = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = vi.inflate(Resource, null);
holder = new ViewHolder();
holder.tvName = (TextView)convertView.findViewById(R.id.tvName);
holder.tvDescription = (TextView)convertView.findViewById(R.id.tvDescriptionn);
holder.tvDob = (TextView)convertView.findViewById(R.id.tvDateOfBirth);
holder.tvCountry = (TextView)convertView.findViewById(R.id.tvCountry);
holder.tvHeight = (TextView)convertView.findViewById(R.id.tvHeight);
holder.tvSpouse = (TextView)convertView.findViewById(R.id.tvSpouse);
holder.tvChildren = (TextView)convertView.findViewById(R.id.tvChildren);
holder.imageView = (ImageView)convertView.findViewById(R.id.ivImage);
convertView.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
new DownloadImageTask(holder.imageView)
.execute(ArrayListActors.get(position).getImage());
holder.tvName.setText(ArrayListActors.get(position).getName());
holder.tvDescription.setText(ArrayListActors.get(position).getDescription());
holder.tvDob.setText(ArrayListActors.get(position).getDob());
holder.tvCountry.setText(ArrayListActors.get(position).getCountry());
holder.tvHeight.setText(ArrayListActors.get(position).getHeight());
holder.tvSpouse.setText(ArrayListActors.get(position).getSpouse());
holder.tvChildren.setText(ArrayListActors.get(position).getChildren());
return convertView;
}
static class ViewHolder{
public TextView tvName;
public TextView tvDescription;
public TextView tvDob;
public TextView tvCountry;
public TextView tvHeight;
public TextView tvSpouse;
public TextView tvChildren;
public ImageView imageView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="#F1F1F1"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/row" >
</ListView>
</LinearLayout>
行XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tom Cruise"
android:textColor="#166CED"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvDateOfBirth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#D64530"
android:text="Date of Birth: July 3, 1962" />
<TextView
android:id="@+id/tvHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Height: 1.80 m"
android:textColor="#D64530"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#D64530"
android:text="United States" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/tvDescriptionn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#009A57"
android:text="Description" />
<TextView
android:id="@+id/tvSpouse"
android:layout_width="wrap_content" android:textColor="#166CED"
android:layout_height="wrap_content"
android:text="Spouse: Katie Holmes" />
<TextView
android:id="@+id/tvChildren"
android:layout_width="wrap_content" android:textColor="#166CED"
android:layout_height="wrap_content"
android:text="Children: Suri Cruise, Isabella Jane Cruise, Connor Cruise" />
</LinearLayout>
非常感謝! HttpGet而不是HttpPost就足夠了。當我有更多時間時,我會仔細研究其餘的建議。現在,說實話,我並不完全理解我應該在哪裏實現您提供的代碼,以及我應該刪除哪些代碼。 – Theodore 2014-09-20 12:30:56