根据本教程,我正在使用改造和GSON将JSON序列化到POJO中。一切都正常工作。为了将实例保存到SQLite数据库中,我使用了糖ORM。我的POJO有字段-整数id。
@Generated("org.jsonschema2pojo")
public class Item extends SugarRecord{
@SerializedName("login")
@Expose
private String login;
@SerializedName("id")
@Expose
private Long id;
Sugar ORM要求整数id应为Long。我说“好的”,然后说了很长时间。当我运行应用程序时,会出现以下错误:
01-18 06:03:44.436 4498-4498/uz.cp.retrofitsandboxE/Android运行时:致命异常:主进程:uz.cp.retrofitsandbox,PID:4498java.lang.运行时异常:无法启动活动ComponentInfo{uz.cp.retrofitsandbox/uz.cp.retrofitsandbox.MainActivity}:java.lang.IllegalArgument异常:无法为类uz.cp.retrofitsandbox.GitResult方法GitApiInterface.get用户NamedTom在android.app.ActivityThread.perform启动活动(ActivityThread.java:2298)在android.app.ActivityThread.handle启动活动(ActivityThread.java:2360)在android.app.ActivityThread.access800美元(ActivityThread.java:144)在android.app.ActivityThread$H. handleMessage(ActivityThread.java:1278)在android.os.Handler.dispatch消息(Handler. java: 102)在android. os. Looper. Loop(Looper. java: 135)在android. app. ActivityThread. main(ActivityThread. java: 5221)在java. lang. reect。方法。调用(本机方法)在java. lang. reect。方法。调用(方法。java: 372)在com. android. interal. os。ZygoteInit$MEodAndArgsCaller. run(ZygoteInit. java: 899)在com. android. interal. os。ZygoteInit. main(ZygoteInit. java: 694)引起的:java. lang。非法Argument异常:无法创建转换器在java. lang. index上的Retrofit1 Dollar. invoke(Retrofit. java: 127)。代理。调用(Proxy. java: 397)在$Proxy0. getUsersNamedTom(未知来源)在uz. cp. Retfitsandbox。在android. app上创建(MainActivity. java: 27)。在android. app上创建(Activity. java: 5933)。在android. app上。仪器。在android. app上调用ActivityOnCreate(Instrumentation. java: 1105)。ActivityThread. performLaunchActive(ActivityThread. java: 2251)。...10更多原因:java. lang。IllegalArgumentExctive:类uz. cp. Retfitsandbox。项目声明多个JSON字段,名为id,位于com. google. gson. interal. bind。反射类型适配器工厂。getBoundFielelyGsonConverterFactory. get(GsonConverterFactory. java: 50)在改造。改造时的Utils。解决方案转换器(Utils. java: 72)。方法处理程序。创建响应转换器(方法处理程序。java: 65)...19更多
这个错误将我引向这一行(27):
Call<GitResult> call = service.getUsersNamedTom("tom");
所以我的问题是:为什么会出现这个错误?如何解决这个问题?
build.gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "uz.cp.retrofitsandbox"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
compile 'org.glassfish.main:javax.annotation:4.0-b33'
compile 'com.github.satyan:sugar:1.4'
}
onCreate(MainActivity.java):
String baseUrl = "https://api.github.com" ;
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
GitApiInterface service = client.create(GitApiInterface.class);
Call<GitResult> call = service.getUsersNamedTom("tom");
call.enqueue(new Callback<GitResult>() {
@Override
public void onResponse(Response<GitResult> response) {
if (response.isSuccess()) {
GitResult result = response.body();
result.save();
GitResult gitResult = GitResult.first(GitResult.class);
Toast.makeText(getApplicationContext(), String.valueOf(gitResult.getItems().size()), Toast.LENGTH_LONG).show();
} else {
//request not successful (like 400,401,403 etc)
//Handle errors
}
}
@Override
public void onFailure(Throwable t) {
}
});
Gitapi接口。JAVA
package uz.cp.retrofitsandbox;
import retrofit.Call;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.POST;
import retrofit.http.PUT;
import retrofit.http.Path;
import retrofit.http.Query;
/**
* Created by Joe on 1/18/2016.
*/
public interface GitApiInterface {
@GET("/search/users")
Call<GitResult> getUsersNamedTom(@Query("q") String name);
@POST("/user/create")
Call<Item> createUser(@Body String name, @Body String email);
@PUT("/user/{id}/update")
Call<Item> updateUser(@Path("id") String id , @Body Item user);
}
GitResult.java
package uz.cp.retrofitsandbox;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.orm.SugarRecord;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
@Generated("org.jsonschema2pojo")
public class GitResult extends SugarRecord{
@SerializedName("total_count")
@Expose
private Integer totalCount;
@SerializedName("incomplete_results")
@Expose
private Boolean incompleteResults;
@SerializedName("items")
@Expose
private List<Item> items = new ArrayList<Item>();
/**
* No args constructor for use in serialization
*
*/
public GitResult() {
}
/**
*
* @param items
* @param totalCount
* @param incompleteResults
*/
public GitResult(Integer totalCount, Boolean incompleteResults, List<Item> items) {
this.totalCount = totalCount;
this.incompleteResults = incompleteResults;
this.items = items;
}
/**
*
* @return
* The totalCount
*/
public Integer getTotalCount() {
return totalCount;
}
/**
*
* @param totalCount
* The total_count
*/
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
/**
*
* @return
* The incompleteResults
*/
public Boolean getIncompleteResults() {
return incompleteResults;
}
/**
*
* @param incompleteResults
* The incomplete_results
*/
public void setIncompleteResults(Boolean incompleteResults) {
this.incompleteResults = incompleteResults;
}
/**
*
* @return
* The items
*/
public List<Item> getItems() {
return items;
}
/**
*
* @param items
* The items
*/
public void setItems(List<Item> items) {
this.items = items;
}
}
项目JAVA
package uz.cp.retrofitsandbox;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.orm.SugarRecord;
import javax.annotation.Generated;
@Generated("org.jsonschema2pojo")
public class Item extends SugarRecord{
@SerializedName("login")
@Expose
private String login;
@SerializedName("id")
@Expose
private Long id;
@SerializedName("avatar_url")
@Expose
private String avatarUrl;
@SerializedName("gravatar_id")
@Expose
private String gravatarId;
@SerializedName("url")
@Expose
private String url;
@SerializedName("html_url")
@Expose
private String htmlUrl;
@SerializedName("followers_url")
@Expose
private String followersUrl;
@SerializedName("following_url")
@Expose
private String followingUrl;
@SerializedName("gists_url")
@Expose
private String gistsUrl;
@SerializedName("starred_url")
@Expose
private String starredUrl;
@SerializedName("subscriptions_url")
@Expose
private String subscriptionsUrl;
@SerializedName("organizations_url")
@Expose
private String organizationsUrl;
@SerializedName("repos_url")
@Expose
private String reposUrl;
@SerializedName("events_url")
@Expose
private String eventsUrl;
@SerializedName("received_events_url")
@Expose
private String receivedEventsUrl;
@SerializedName("type")
@Expose
private String type;
@SerializedName("site_admin")
@Expose
private Boolean siteAdmin;
@SerializedName("score")
@Expose
private Double score;
/**
* No args constructor for use in serialization
*
*/
public Item() {
}
/**
*
* @param eventsUrl
* @param siteAdmin
* @param gistsUrl
* @param score
* @param type
* @param gravatarId
* @param url
* @param subscriptionsUrl
* @param id
* @param followersUrl
* @param reposUrl
* @param htmlUrl
* @param receivedEventsUrl
* @param avatarUrl
* @param followingUrl
* @param login
* @param organizationsUrl
* @param starredUrl
*/
public Item(String login, Long id, String avatarUrl, String gravatarId, String url, String htmlUrl, String followersUrl, String followingUrl, String gistsUrl, String starredUrl, String subscriptionsUrl, String organizationsUrl, String reposUrl, String eventsUrl, String receivedEventsUrl, String type, Boolean siteAdmin, Double score) {
this.login = login;
this.id = id;
this.avatarUrl = avatarUrl;
this.gravatarId = gravatarId;
this.url = url;
this.htmlUrl = htmlUrl;
this.followersUrl = followersUrl;
this.followingUrl = followingUrl;
this.gistsUrl = gistsUrl;
this.starredUrl = starredUrl;
this.subscriptionsUrl = subscriptionsUrl;
this.organizationsUrl = organizationsUrl;
this.reposUrl = reposUrl;
this.eventsUrl = eventsUrl;
this.receivedEventsUrl = receivedEventsUrl;
this.type = type;
this.siteAdmin = siteAdmin;
this.score = score;
}
/**
*
* @return
* The login
*/
public String getLogin() {
return login;
}
/**
*
* @param login
* The login
*/
public void setLogin(String login) {
this.login = login;
}
/**
*
* @return
* The id
*/
public Long getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Long id) {
this.id = id;
}
/**
*
* @return
* The avatarUrl
*/
public String getAvatarUrl() {
return avatarUrl;
}
/**
*
* @param avatarUrl
* The avatar_url
*/
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
/**
*
* @return
* The gravatarId
*/
public String getGravatarId() {
return gravatarId;
}
/**
*
* @param gravatarId
* The gravatar_id
*/
public void setGravatarId(String gravatarId) {
this.gravatarId = gravatarId;
}
/**
*
* @return
* The url
*/
public String getUrl() {
return url;
}
/**
*
* @param url
* The url
*/
public void setUrl(String url) {
this.url = url;
}
/**
*
* @return
* The htmlUrl
*/
public String getHtmlUrl() {
return htmlUrl;
}
/**
*
* @param htmlUrl
* The html_url
*/
public void setHtmlUrl(String htmlUrl) {
this.htmlUrl = htmlUrl;
}
/**
*
* @return
* The followersUrl
*/
public String getFollowersUrl() {
return followersUrl;
}
/**
*
* @param followersUrl
* The followers_url
*/
public void setFollowersUrl(String followersUrl) {
this.followersUrl = followersUrl;
}
/**
*
* @return
* The followingUrl
*/
public String getFollowingUrl() {
return followingUrl;
}
/**
*
* @param followingUrl
* The following_url
*/
public void setFollowingUrl(String followingUrl) {
this.followingUrl = followingUrl;
}
/**
*
* @return
* The gistsUrl
*/
public String getGistsUrl() {
return gistsUrl;
}
/**
*
* @param gistsUrl
* The gists_url
*/
public void setGistsUrl(String gistsUrl) {
this.gistsUrl = gistsUrl;
}
/**
*
* @return
* The starredUrl
*/
public String getStarredUrl() {
return starredUrl;
}
/**
*
* @param starredUrl
* The starred_url
*/
public void setStarredUrl(String starredUrl) {
this.starredUrl = starredUrl;
}
/**
*
* @return
* The subscriptionsUrl
*/
public String getSubscriptionsUrl() {
return subscriptionsUrl;
}
/**
*
* @param subscriptionsUrl
* The subscriptions_url
*/
public void setSubscriptionsUrl(String subscriptionsUrl) {
this.subscriptionsUrl = subscriptionsUrl;
}
/**
*
* @return
* The organizationsUrl
*/
public String getOrganizationsUrl() {
return organizationsUrl;
}
/**
*
* @param organizationsUrl
* The organizations_url
*/
public void setOrganizationsUrl(String organizationsUrl) {
this.organizationsUrl = organizationsUrl;
}
/**
*
* @return
* The reposUrl
*/
public String getReposUrl() {
return reposUrl;
}
/**
*
* @param reposUrl
* The repos_url
*/
public void setReposUrl(String reposUrl) {
this.reposUrl = reposUrl;
}
/**
*
* @return
* The eventsUrl
*/
public String getEventsUrl() {
return eventsUrl;
}
/**
*
* @param eventsUrl
* The events_url
*/
public void setEventsUrl(String eventsUrl) {
this.eventsUrl = eventsUrl;
}
/**
*
* @return
* The receivedEventsUrl
*/
public String getReceivedEventsUrl() {
return receivedEventsUrl;
}
/**
*
* @param receivedEventsUrl
* The received_events_url
*/
public void setReceivedEventsUrl(String receivedEventsUrl) {
this.receivedEventsUrl = receivedEventsUrl;
}
/**
*
* @return
* The type
*/
public String getType() {
return type;
}
/**
*
* @param type
* The type
*/
public void setType(String type) {
this.type = type;
}
/**
*
* @return
* The siteAdmin
*/
public Boolean getSiteAdmin() {
return siteAdmin;
}
/**
*
* @param siteAdmin
* The site_admin
*/
public void setSiteAdmin(Boolean siteAdmin) {
this.siteAdmin = siteAdmin;
}
/**
*
* @return
* The score
*/
public Double getScore() {
return score;
}
/**
*
* @param score
* The score
*/
public void setScore(Double score) {
this.score = score;
}
}
在你的课堂上。cp.沙箱。Item您有多个JSON名称为id
的字段。确保映射是一对一的。
我想使用executeQuery mongodb驱动程序php搜索_id。 这是我的用户集合的文档结构 而我的php代码是 当我运行它时,我发现以下错误 PHP致命错误:未捕获异常“MongoDB\Driver\exception\ConnectionException”,在/测试中显示消息“未知运算符:$id”。php:27堆栈跟踪:0/测试。php(27):MongoDB\Driver\Man
嗨,我在java 8中运行外部进程有问题。基本上,我可以从我的java代码运行ffplay,如果外部进程有子进程,似乎.毁()方法不起作用。 我的代码是:'' ''但即使在执行销毁()和强制销毁()之后,ffplay窗口仍保持打开状态。我试着使用Runtime。getRuntime()。执行官(……)而不是ProcessBuilder,在本例中,销毁方法似乎会关闭ffplay窗口。但我不知道如何在
我有Byte Buddy作为代理运行,它成功地拦截了我的绝大多数代码库,顺便说一下,这是相当大的!虽然有几个异常值,我不能测量,我在下面记录,希望你能知道答案! 1.CGLIB生成的类 Spring生成了一些额外的类,它们与我的类同名,但在末尾附加了,这些会导致错误。我得到的错误是: 2.打包私密类和私密类 我看到的另一个问题是检测或。 代码如下所示: Byte Buddy instrument或
我有一个数十万对象的列表。当每一个运行时,它都会根据给定的值执行一个可能很长的计算。正因为如此,我希望异步运行每个任务(最好是通过使用某种执行器),并在30秒后检索每次计算的结果,取消那些没有及时完成的结果。(所得值在其他地方使用。) 到目前为止,我就是这样实现它的: ArrayList存储每个要执行的,然后将其发送到ExecutorService以运行所有任务。我遇到的问题是,任务似乎是同步启动
问题内容: 我在应用程序中使用Gson,为此,我使用了一些名称与使用Json相同的类。我的应用程序运行良好,但是在编写proguard时,应用程序崩溃了,我猜有些类正在缩小。我的错误是: java.lang.ClassCastException:com.google.gson.internal.StringMap无法转换为com.sample.package.GsonClass 问题答案: 您需要
我似乎无法使用Excel::Writer::XLSX将超过85,000行导出到. xlsx文件。导出超过85,000行数据会导致5KB文件中没有数据。当导出85,000条记录或90,000条记录(约40秒)时,脚本运行所需的时间大致相同。 85000行的文件是7.9MB,但90000行的文件只有5KB。 使用top监视导出脚本,我的perl脚本只显示大约1%的内存使用率,几秒钟后就会消失。我不熟悉