Created
October 12, 2012 02:15
-
-
Save kimukou/3876967 to your computer and use it in GitHub Desktop.
JPP_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
☆1 の段階で | |
net.vvakame.util.jsonpullparser.JsonFormatException: not started '('!, Do you want the json hash? | |
☆2 だとエラーは出なくなるが1個しか取れないよう・・・(10個は取れるはず | |
☆3 だと下記のエラー(treatUnknownKeyAsError = true つけると全マッピングする必要あり) | |
net.vvakame.util.jsonpullparser.JsonFormatException: unsupported key. key=user_id | |
とか出てしまう | |
note) | |
treatUnknownKeyAsError = true を指定するとマッピングエラーの時点でExceptionを発生 | |
<指定しない場合は読み捨て | |
うまくマッピングできないときは | |
@JsonKey("package_name") | |
String packageName; | |
な指定も可能 | |
https://github.com/vvakame/JsonPullParser/blob/master/jsonpullparser-usage/src/main/java/net/vvakame/sample/TestData.java#L26 | |
decamelize = false ・・厳密なキー名でマップ | |
decamelize = true ・・_等を似た変数でマップ (short_id =>shortId 等) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TwitPicDownloadTask extends AsyncTask<Void, TwitPicRootBean, Void> { | |
private boolean exception_f = false; | |
OnJsonObjectAddListener listener = new OnJsonObjectAddListener() { | |
@Override | |
public void onAdd(Object obj) { | |
if (obj instanceof TwitPicRootBean) { | |
TwitPicRootBean bean = (TwitPicRootBean) obj; | |
publishProgress(bean); | |
} | |
} | |
}; | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
adapter.clear(); | |
exception_f = false; | |
} | |
@Override | |
protected Void doInBackground(Void... params) { | |
HttpURLConnection urlConnection = null; | |
try { | |
String url_twitpic = "http://api.twitpic.com/2/tags/show.json?tag=nyaruko&page=1"; | |
URL url = new URL(url_twitpic); | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
//TwitPicRootBeanGererated.getList(urlConnection.getInputStream(), listener);//☆1 | |
//TwitPicImageBeanGererated.get(urlConnection.getInputStream(), listener);//☆2 | |
TwitPicRootBeanGererated.get(urlConnection.getInputStream(), listener);//☆3 | |
//JsonPullParser parser = JsonPullParser.newParser(urlConnection.getInputStream(), "ISO-8859-1"); | |
//JsonPullParser parser = JsonPullParser.newParser(urlConnection.getInputStream(), "UTF-8"); | |
//TwitPicRootBeanGererated.get(parser,listener); | |
} catch (Exception e) { | |
LogUtil.error(TAG,"JsonPullParser", e); | |
exception_f = true; | |
cancel(true); | |
} | |
finally{ | |
if(urlConnection!=null)urlConnection.disconnect(); | |
} | |
return null; | |
} | |
@Override | |
protected void onCancelled(Void result) { | |
// TODO 自動生成されたメソッド・スタブ | |
if(exception_f){ | |
Toast.makeText(activity, "exception", Toast.LENGTH_LONG).show(); | |
//砂時計止める等の処理 | |
} | |
} | |
// メインスレッドで実行する処理 | |
@Override | |
protected void onPostExecute(Void result) { | |
if(exception_f){ | |
Toast.makeText(activity, "exception", Toast.LENGTH_LONG).show(); | |
//砂時計止める等の処理 | |
} | |
} | |
@Override | |
protected void onProgressUpdate(TwitPicRootBean... progress) { | |
for (TwitPicRootBean bean : progress) { | |
for (TwitPicImageBean bean_s : bean.getImages()) { | |
adapter.add(beans_s); | |
adapter.notifyDataSetChanged(); | |
} | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.vvakame.util.jsonpullparser.annotation.JsonKey; | |
import net.vvakame.util.jsonpullparser.annotation.JsonModel; | |
@JsonModel(decamelize = true)//,treatUnknownKeyAsError = true) | |
public class TwitPicImageBean { | |
@JsonKey | |
private String id; | |
@JsonKey | |
private String shortId; | |
@JsonKey | |
private String message; | |
@JsonKey | |
private String timestamp; | |
@JsonKey | |
private UserBean user; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getShortId() { | |
return shortId; | |
} | |
public void setShortId(String shortId) { | |
this.shortId = shortId; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
public String getTimestamp() { | |
return timestamp; | |
} | |
public void setTimestamp(String timestamp) { | |
this.timestamp = timestamp; | |
} | |
public UserBean getUser() { | |
return user; | |
} | |
public void setUser(UserBean user) { | |
this.user = user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import net.vvakame.util.jsonpullparser.annotation.JsonKey; | |
import net.vvakame.util.jsonpullparser.annotation.JsonModel; | |
@JsonModel(decamelize = false)//,treatUnknownKeyAsError = true) | |
public class TwitPicRootBean { | |
@JsonKey | |
private String id; | |
@JsonKey | |
private String name; | |
@JsonKey | |
private List<TwitPicImageBean> images; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public List<TwitPicImageBean> getImages() { | |
return images; | |
} | |
public void setImages(List<TwitPicImageBean> images) { | |
this.images = images; | |
} | |
//[TODO]同じAsyncTaskで http://api.twitpic.com/2/users/show.json も兼ねたとき | |
//例)http://api.twitpic.com/2/users/show.json?username=Figure_No_Oni用 | |
@JsonKey | |
private String username; | |
@JsonKey | |
private String bio; | |
@JsonKey | |
private String avatarUrl; | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getBio() { | |
return bio; | |
} | |
public void setBio(String bio) { | |
this.bio = bio; | |
} | |
public String getAvatarUrl() { | |
return avatarUrl; | |
} | |
public void setAvatarUrl(String avatarUrl) { | |
this.avatarUrl = avatarUrl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.vvakame.util.jsonpullparser.annotation.JsonKey; | |
import net.vvakame.util.jsonpullparser.annotation.JsonModel; | |
@JsonModel(decamelize = true)//,treatUnknownKeyAsError = true) | |
public class UserBean { | |
@JsonKey | |
private String id; | |
@JsonKey | |
private String name; | |
@JsonKey | |
private String username; | |
@JsonKey | |
private String bio; | |
@JsonKey | |
private String avatarUrl; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getBio() { | |
return bio; | |
} | |
public void setBio(String bio) { | |
this.bio = bio; | |
} | |
public String getAvatarUrl() { | |
return avatarUrl; | |
} | |
public void setAvatarUrl(String avatarUrl) { | |
this.avatarUrl = avatarUrl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment