gpt4 book ai didi

java - 安卓 : YelpAPI access error

转载 作者:行者123 更新时间:2023-11-30 10:44:58 31 4
gpt4 key购买 nike

我想通过 Yelp API 显示使用我当前位置的餐馆列表。对此几乎没有或没有明确的帮助。我正在尝试各种可能的方法来获得结果但无法获得结果。我只是 Android 的初学者。

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'. com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/apache/commons/codec/BinaryDecoder.class

流程应该类似于 MainActivityFragment,它应该显示距离我当前位置一英里范围内的餐馆列表。

MAWFragment.java 类

package com.nickdroid.projectmaw;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.yelp.clientlib.connection.YelpAPI;
import com.yelp.clientlib.connection.YelpAPIFactory;
import com.yelp.clientlib.entities.SearchResponse;

import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

;

/**
* A placeholder fragment containing a simple view.
*/
public class MAWFragment extends Fragment {

public static final String LOG_TAG = "MAWFragment";
private ArrayAdapter<String> adapter;
/*final String CONSUMER_KEY = "xxxxxxxxxxxxxx";
final String CONSUMER_SECRET = "xxxxxxxxxxxxxx";
final String TOKEN = "xxxxxxxxxxxxxxx";
final String TOKEN_SECRET = "xxxxxxxxxxxxxxxxx";*/

final String YELP_BASEURL="https://api.yelp.com/v2/search?";
final String Y_TERM = "term";
final String Y_LOCATION = "location";
final int Y_LIMIT = 10;

public MAWFragment() {
}


@Override
public void onStart() {
super.onStart();

}



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

/*String[] weeksWeather = {"sunny","cloudy","hot","sleet","volcanic ash","windy","tornado","tsunami","breezy"};
ArrayList<String> weatherArray = new ArrayList<String>(Arrays.asList(weeksWeather));
*/
YelpAPIFactory apiFactory = new YelpAPIFactory(BuildConfig.CONSUMER_KEY, BuildConfig.CONSUMER_SECRET, BuildConfig.TOKEN, BuildConfig.TOKEN_SECRET);
YelpAPI yelpAPI = apiFactory.createAPI();
Map<String, String> params = new HashMap<>();

// general params
params.put("limit", "10");
// locale params
params.put("lang", "en");

try {
Call<SearchResponse> call = yelpAPI.getPhoneSearch("San Francisco", params);
SearchResponse searchResponse = call.execute().body();
}catch(Exception ioEception){
ioEception.printStackTrace();
}

final ListView listView = (ListView)rootView.findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(getActivity(),R.layout.fragment_main,R.id.item_text,new ArrayList<String>());


listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemPosition = position;
String itemValue = (String) listView.getItemAtPosition(position);

Toast.makeText(getActivity(), "ListItem : " + itemValue, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}

OAuthService service;
Token accessToken;

/*public MAWFragment(String consumerKey, String consumerSecret, String token, String tokenSecret) {
this.service = new ServiceBuilder().provider(TwoStepOAuth.class).apiKey(consumerKey).apiSecret(consumerSecret)
.build();
this.accessToken = new Token(token, tokenSecret);
}*/

public String searchForBusinessesByLocation(String term, String location) {
OAuthRequest request = createOAuthRequest(YELP_BASEURL);
request.addQuerystringParameter("term", Y_TERM);
request.addQuerystringParameter("location", Y_LOCATION);
request.addQuerystringParameter("limit", String.valueOf(Y_LIMIT));
return sendRequestAndGetResponse(request);
}


private OAuthRequest createOAuthRequest(String path) {
OAuthRequest request = new OAuthRequest(Verb.GET, path);
return request;
}

private String sendRequestAndGetResponse(OAuthRequest request) {
System.out.println("Querying " + request.getCompleteUrl() + " ...");
this.service.signRequest(this.accessToken, request);
Response response = request.send();
return response.getBody();
}

public class FetchMealsTask extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... data) {

return null;
}

}
}

主 Activity .java

package com.nickdroid.projectmaw;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

构建.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "com.nickdroid.niket.projectmaw"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}

buildTypes.each {
it.buildConfigField 'String', 'CONSUMER_KEY', MyYelpMAWApiKey
it.buildConfigField 'String', 'CONSUMER_SECRET', MyYelpMAWSecretKey
it.buildConfigField 'String', 'TOKEN', MyYelpMAWToken
it.buildConfigField 'String', 'TOKEN_SECRET', MyYelpMAWSecretToken
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile files('libs/scribe-1.3.0.jar')
compile files('libs/commons-codec-1.7.jar')
compile files('libs/yelp-android-2.0.0.jar')
compile 'se.akerfeldt:okhttp-signpost:1.1.0'
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
compile 'oauth.signpost:signpost-core:1.2.1.2'
compile files('libs/converter-jackson-2.0.0.jar')
compile 'com.android.support:multidex:1.0.0'
compile files('libs/retrofit-2.0.2.jar')
}

android {
configurations{
all*.exclude module: 'commons-codec-1.7.jar'
}
}

gradle.properties

# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

MyYelpMAWApiKey = "xxxxxxxxxxxxxx"
MyYelpMAWSecretKey = "xxxxxxxxxx"
MyYelpMAWSecretToken = "xxxxxxxxx"
MyYelpMAWToken = "xxxxxxxxx"

最佳答案

我能够按如下方式处理这个问题:

  • 为了解决冲突,我删除了项目中的 Apache JAR。
  • 为确保正确重新添加所需的库,我将此行添加到 build.gradle 中的“android”下:

    useLibrary 'org.apache.http.legacy'
  • 为了处理我不需要的文件的不同冲突,我也在“android”下添加了这个部分:

    packagingOptions {
    exclude 'META-INF/maven/oauth.signpost/signpost-core/pom.properties'
    }

然后我的项目就建立了。

关于java - 安卓 : YelpAPI access error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37235398/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com