Java 从Tumblr解析JSONObject
我试图教自己制作android应用程序,因此我试图创建一个应用程序,在列表视图中显示tumblr帐户中的图像 我在解析JSONObject时遇到一些问题,我的应用程序因为nullPointerException而崩溃 我的代码如下所示:Java 从Tumblr解析JSONObject,java,android,json,parsing,tumblr,Java,Android,Json,Parsing,Tumblr,我试图教自己制作android应用程序,因此我试图创建一个应用程序,在列表视图中显示tumblr帐户中的图像 我在解析JSONObject时遇到一些问题,我的应用程序因为nullPointerException而崩溃 我的代码如下所示: public class Example extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan
public class Example extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<Tweet> tweets;
try {
tweets = getTweets();
ListView listView = (ListView) findViewById(R.id.ListViewId);
listView.setAdapter(new UserItemAdapter(this, R.layout.listitem,
tweets));
} 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();
}
}
public class UserItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter(Context context, int imageViewResourceId,
ArrayList<Tweet> tweets) {
super(context, imageViewResourceId, tweets);
this.tweets = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
ImageView image = (ImageView) v.findViewById(R.id.avatar);
if (image != null) {
image.setImageBitmap(getBitmap(tweet.image_url));
}
}
return v;
}
}
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (Exception ex) {
return null;
}
}
public ArrayList<Tweet> getTweets() throws ClientProtocolException,
IOException, JSONException {
String searchUrl = "http://api.tumblr.com/v2/blog/www.richkidsofinstagram.tumblr.com/posts?api_key=API_KEY";
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try {
responseBody = client.execute(get, responseHandler);
} catch (Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = new JSONObject(responseBody);
JSONArray arr = null;
try {
arr = jsonObject.getJSONArray("results");
} catch (Exception ex) {
Log.v("TEST", "Exception: " + ex.getMessage());
}
for (int i = 0; i < arr.length(); i++) {
Tweet tweet = new Tweet(arr.getJSONObject(i).getString("photos"));
tweets.add(tweet);
}
return tweets;
}
public class Tweet {
public String image_url;
public Tweet(String url) {
this.image_url = url;
}
}
}
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"blog": {
"title": "Rich Kids Of Instagram",
"posts": 154,
"name": "richkidsofinstagram",
"url": "http://richkidsofinstagram.tumblr.com/",
"updated": 1346803265,
"description": "They have more money than you and this is what they do.
"ask": true,
"ask_anon": true
},
"posts": [
{
"blog_name": "richkidsofinstagram",
"id": 30900248446,
"post_url": "http://richkidsofinstagram.tumblr.com/post/30900248446/hamptons-are-good",
"slug": "hamptons-are-good",
"type": "photo",
"date": "2012-09-04 23:58:45 GMT",
"timestamp": 1346803125,
"state": "published",
"format": "html",
"reblog_key": "2KosMjea",
"tags": [
"pool",
"hamptons",
"summer",
"rich",
"wealth"
],
"highlighted": [],
"note_count": 99,
"caption": "<p><span>The Hamptons are…….. good. by matthewmorton</span></p>",
"photos": [
{
"caption": "",
"alt_sizes": [
{
"width": 500,
"height": 500,
"url": "http://24.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_500.jpg"
},
{
"width": 400,
"height": 400,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_400.jpg"
},
{
"width": 250,
"height": 250,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_250.jpg"
},
{
"width": 100,
"height": 100,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_100.jpg"
},
{
"width": 75,
"height": 75,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_75sq.jpg"
}
],
"original_size": {
"width": 500,
"height": 500,
"url": "http://24.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_500.jpg"
}
}
]
},
公共类示例扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList推文;
试一试{
tweets=getTweets();
ListView ListView=(ListView)findViewById(R.id.ListViewId);
setAdapter(新的UserItemAdapter(这个,R.layout.listitem,
推特);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
公共类UserItemAdapter扩展了ArrayAdapter{
私人ArrayList推文;
公共UserItemAdapter(上下文上下文,int-imageViewResourceId,
ArrayList推文){
超级(上下文、imageViewResourceId、tweets);
this.tweets=tweets;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.layout.listitem,空);
}
Tweet=tweets.get(位置);
if(tweet!=null){
ImageView图像=(ImageView)v.findViewById(R.id.avatar);
如果(图像!=null){
setImageBitmap(getBitmap(tweet.image_url));
}
}
返回v;
}
}
公共位图getBitmap(字符串bitmapUrl){
试一试{
URL URL=新URL(位图URL);
返回BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
}捕获(例外情况除外){
返回null;
}
}
public ArrayList getTweets()抛出ClientProtocolException,
IOException,jsoneexception{
字符串搜索URL=”http://api.tumblr.com/v2/blog/www.richkidsofinstagram.tumblr.com/posts?api_key=API_KEY";
ArrayList tweets=新建ArrayList();
HttpClient=new DefaultHttpClient();
HttpGet=newhttpget(searchUrl);
ResponseHandler ResponseHandler=新BasicResponseHandler();
字符串responseBody=null;
试一试{
responseBody=client.execute(get,responseHandler);
}捕获(例外情况除外){
例如printStackTrace();
}
JSONObject JSONObject=新的JSONObject(ResponseBy);
JSONArray arr=null;
试一试{
arr=jsonObject.getJSONArray(“结果”);
}捕获(例外情况除外){
Log.v(“测试”,“异常:+ex.getMessage());
}
对于(int i=0;i
nullPointerException发生在第124行:
for (int i = 0; i < arr.length(); i++) {
Tweet tweet = new Tweet(arr.getJSONObject(i).getString("photos"));
tweets.add(tweet);
}
for(int i=0;i
我还通过www.jsonlint.com验证了地址,JSON如下所示:
public class Example extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<Tweet> tweets;
try {
tweets = getTweets();
ListView listView = (ListView) findViewById(R.id.ListViewId);
listView.setAdapter(new UserItemAdapter(this, R.layout.listitem,
tweets));
} 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();
}
}
public class UserItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter(Context context, int imageViewResourceId,
ArrayList<Tweet> tweets) {
super(context, imageViewResourceId, tweets);
this.tweets = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
ImageView image = (ImageView) v.findViewById(R.id.avatar);
if (image != null) {
image.setImageBitmap(getBitmap(tweet.image_url));
}
}
return v;
}
}
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (Exception ex) {
return null;
}
}
public ArrayList<Tweet> getTweets() throws ClientProtocolException,
IOException, JSONException {
String searchUrl = "http://api.tumblr.com/v2/blog/www.richkidsofinstagram.tumblr.com/posts?api_key=API_KEY";
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try {
responseBody = client.execute(get, responseHandler);
} catch (Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = new JSONObject(responseBody);
JSONArray arr = null;
try {
arr = jsonObject.getJSONArray("results");
} catch (Exception ex) {
Log.v("TEST", "Exception: " + ex.getMessage());
}
for (int i = 0; i < arr.length(); i++) {
Tweet tweet = new Tweet(arr.getJSONObject(i).getString("photos"));
tweets.add(tweet);
}
return tweets;
}
public class Tweet {
public String image_url;
public Tweet(String url) {
this.image_url = url;
}
}
}
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"blog": {
"title": "Rich Kids Of Instagram",
"posts": 154,
"name": "richkidsofinstagram",
"url": "http://richkidsofinstagram.tumblr.com/",
"updated": 1346803265,
"description": "They have more money than you and this is what they do.
"ask": true,
"ask_anon": true
},
"posts": [
{
"blog_name": "richkidsofinstagram",
"id": 30900248446,
"post_url": "http://richkidsofinstagram.tumblr.com/post/30900248446/hamptons-are-good",
"slug": "hamptons-are-good",
"type": "photo",
"date": "2012-09-04 23:58:45 GMT",
"timestamp": 1346803125,
"state": "published",
"format": "html",
"reblog_key": "2KosMjea",
"tags": [
"pool",
"hamptons",
"summer",
"rich",
"wealth"
],
"highlighted": [],
"note_count": 99,
"caption": "<p><span>The Hamptons are…….. good. by matthewmorton</span></p>",
"photos": [
{
"caption": "",
"alt_sizes": [
{
"width": 500,
"height": 500,
"url": "http://24.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_500.jpg"
},
{
"width": 400,
"height": 400,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_400.jpg"
},
{
"width": 250,
"height": 250,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_250.jpg"
},
{
"width": 100,
"height": 100,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_100.jpg"
},
{
"width": 75,
"height": 75,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_75sq.jpg"
}
],
"original_size": {
"width": 500,
"height": 500,
"url": "http://24.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_500.jpg"
}
}
]
},
{
“元”:{
“地位”:200,
“msg”:“好的”
},
“答复”:{
“博客”:{
“标题”:“Instagram的富家子弟”,
“员额”:154,
“名称”:“richkidsofinstagram”,
“url”:”http://richkidsofinstagram.tumblr.com/",
“更新”:1346803265,
“描述”:“他们的钱比你多,他们就是这么做的。
“问”:没错,
“问安”:对
},
“员额”:[
{
“博客名称”:“richkidsofinstagram”,
“id”:30900248446,
“发布url”:http://richkidsofinstagram.tumblr.com/post/30900248446/hamptons-are-good",
“鼻涕虫”:“汉普顿很好”,
“类型”:“照片”,
“日期”:“2012-09-04 23:58:45 GMT”,
“时间戳”:1346803125,
“国家”:“已出版”,
“格式”:“html”,
“重新登录密钥”:“2KosMjea”,
“标签”:[
“游泳池”,
“汉普顿”,
“夏天”,
“富有”,
“财富”
],
“突出显示”:[],
“票据计数”:99,
“说明”:“汉普顿是……好的。作者:马修莫顿””,
“照片”:[
{
“标题”:“,
“alt_尺寸”:[
{
“宽度”:500,
“高度”:500,
“url”:”http://24.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_500.jpg"
},
{
“宽度”:400,
“高度”:400,
“url”:”http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_400.jpg"
},
{
“宽度”:250,
“高度”:250,
“url”:”http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_250.jpg"
},
{
“宽度”:100,
“高度”:100,
“url”:”http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_100.jpg"
},
{
“宽度”:75,
“高度”:75,
“url”:
tweets.add(tweet);