如何在 onPerformSync 中实现 Yelp Api
How to implement Yelp Api inside onPerformSync
你们能帮我推一下吗?我一直在尝试 运行 onPerformSyn 中的 yelp api。
我怎么可能这样做。?我收到以下错误。 "No empty constructor"
我正在使用此 post 来尝试创建构造函数,但它对我不起作用。
请帮助表示赞赏。
注意:在 Yelp 响应之后,我会将数据存储到 SQLite(方案库)中,这样代码就不会结束。
public class SyncAdapter extends AbstractThreadedSyncAdapter{
public final String LOG_TAG = SyncAdapter.class.getSimpleName();
public static final int SYNC_INTERVAL = 4;//60 * 180;
public static final int SYNC_FLEXTIME = SYNC_INTERVAL/3;
private static final long DAY_IN_MILLIS = 1000;//1000 * 60 * 60 * 24;
private static final int WEATHER_NOTIFICATION_ID = 3004;
private GoogleApiClient mGoogleApiClient;
private YelpAPI yelpAPI;
private Location mLastLocation;
public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
// YelpAPIFactory apiFactory = new YelpAPIFactory(BuildConfig.YELP_CONSUMER_KEY, BuildConfig.YELP_CONSUMER_SECRET, BuildConfig.YELP_TOKEN, BuildConfig.YELP_TOKEN_SECRET);
// yelpAPI = apiFactory.createAPI();
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
class YelpApi2 extends DefaultApi10a {
@Override
public String getAccessTokenEndpoint() {
return null;
}
@Override
public String getAuthorizationUrl(Token arg0) {
return null;
}
@Override
public String getRequestTokenEndpoint() {
return null;
}
}
class Yelp {
OAuthService service;
Token accessToken;
public Yelp(String consumerKey, String consumerSecret, String token, String tokenSecret) {
this.service = new ServiceBuilder().provider(YelpApi2.class).apiKey(consumerKey).apiSecret(consumerSecret).build();
this.accessToken = new Token(token, tokenSecret);
}
public String search(String term, double latitude, double longitude) {
OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
request.addQuerystringParameter("term", term);
request.addQuerystringParameter("ll", latitude + "," + longitude);
this.service.signRequest(this.accessToken, request);
Response response = request.send();
return response.getBody();
}
}
Yelp yelp = new Yelp(BuildConfig.YELP_CONSUMER_KEY, BuildConfig.YELP_CONSUMER_SECRET, BuildConfig.YELP_TOKEN, BuildConfig.YELP_TOKEN_SECRET);
String response = yelp.search("church", 30.361471, -87.164326);
System.out.println(response);
}
我关注了这个tut,
但我没有做 .jar 部分,我无法导入它,而是在 gradle.
中使用了这一行
compile 'com.yelp.clientlib:yelp-android:2.0.0'
Error I'm getting
=========================================
FATAL EXCEPTION: SyncAdapterThread-1
Process: mem.edu.joshua, PID: 14149
org.scribe.exceptions.OAuthException: Error while creating the Api object
at org.scribe.builder.ServiceBuilder.createApi(ServiceBuilder.java:59)
at org.scribe.builder.ServiceBuilder.provider(ServiceBuilder.java:45)
at mem.edu.joshua.sync.SyncAdapterYelp.<init>(SyncAdapter.java:99)
at mem.edu.joshua.sync.SyncAdapter.onPerformSync(SyncAdapter.java:116)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)
Caused by: java.lang.InstantiationException: can't instantiate class mem.edu.joshua.sync.SyncAdapterYelpApi2; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
[1]:
[2]: https://thysmichels.com/2011/12/30/yelp-api-example/#comment-18031
问题是 ServiceBuilder 试图实例化 class 槽反射。这将在使用本地或内部 class 时引起问题,因为这些 classed 需要额外的 'invisible' 参数。 (相关问题:How to instantiate inner class with reflection in Java?)
所以解决方案是将 Yelp class 和 YelpApi2 class 提取到它们自己的文件中。
你们能帮我推一下吗?我一直在尝试 运行 onPerformSyn 中的 yelp api。
我怎么可能这样做。?我收到以下错误。 "No empty constructor"
我正在使用此 post 来尝试创建构造函数,但它对我不起作用。
请帮助表示赞赏。
注意:在 Yelp 响应之后,我会将数据存储到 SQLite(方案库)中,这样代码就不会结束。
public class SyncAdapter extends AbstractThreadedSyncAdapter{
public final String LOG_TAG = SyncAdapter.class.getSimpleName();
public static final int SYNC_INTERVAL = 4;//60 * 180;
public static final int SYNC_FLEXTIME = SYNC_INTERVAL/3;
private static final long DAY_IN_MILLIS = 1000;//1000 * 60 * 60 * 24;
private static final int WEATHER_NOTIFICATION_ID = 3004;
private GoogleApiClient mGoogleApiClient;
private YelpAPI yelpAPI;
private Location mLastLocation;
public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
// YelpAPIFactory apiFactory = new YelpAPIFactory(BuildConfig.YELP_CONSUMER_KEY, BuildConfig.YELP_CONSUMER_SECRET, BuildConfig.YELP_TOKEN, BuildConfig.YELP_TOKEN_SECRET);
// yelpAPI = apiFactory.createAPI();
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
class YelpApi2 extends DefaultApi10a {
@Override
public String getAccessTokenEndpoint() {
return null;
}
@Override
public String getAuthorizationUrl(Token arg0) {
return null;
}
@Override
public String getRequestTokenEndpoint() {
return null;
}
}
class Yelp {
OAuthService service;
Token accessToken;
public Yelp(String consumerKey, String consumerSecret, String token, String tokenSecret) {
this.service = new ServiceBuilder().provider(YelpApi2.class).apiKey(consumerKey).apiSecret(consumerSecret).build();
this.accessToken = new Token(token, tokenSecret);
}
public String search(String term, double latitude, double longitude) {
OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
request.addQuerystringParameter("term", term);
request.addQuerystringParameter("ll", latitude + "," + longitude);
this.service.signRequest(this.accessToken, request);
Response response = request.send();
return response.getBody();
}
}
Yelp yelp = new Yelp(BuildConfig.YELP_CONSUMER_KEY, BuildConfig.YELP_CONSUMER_SECRET, BuildConfig.YELP_TOKEN, BuildConfig.YELP_TOKEN_SECRET);
String response = yelp.search("church", 30.361471, -87.164326);
System.out.println(response);
}
我关注了这个tut, 但我没有做 .jar 部分,我无法导入它,而是在 gradle.
中使用了这一行 compile 'com.yelp.clientlib:yelp-android:2.0.0'
Error I'm getting
=========================================
FATAL EXCEPTION: SyncAdapterThread-1
Process: mem.edu.joshua, PID: 14149
org.scribe.exceptions.OAuthException: Error while creating the Api object
at org.scribe.builder.ServiceBuilder.createApi(ServiceBuilder.java:59)
at org.scribe.builder.ServiceBuilder.provider(ServiceBuilder.java:45)
at mem.edu.joshua.sync.SyncAdapterYelp.<init>(SyncAdapter.java:99)
at mem.edu.joshua.sync.SyncAdapter.onPerformSync(SyncAdapter.java:116)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)
Caused by: java.lang.InstantiationException: can't instantiate class mem.edu.joshua.sync.SyncAdapterYelpApi2; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
[1]:
[2]: https://thysmichels.com/2011/12/30/yelp-api-example/#comment-18031
问题是 ServiceBuilder 试图实例化 class 槽反射。这将在使用本地或内部 class 时引起问题,因为这些 classed 需要额外的 'invisible' 参数。 (相关问题:How to instantiate inner class with reflection in Java?)
所以解决方案是将 Yelp class 和 YelpApi2 class 提取到它们自己的文件中。