簡體   English   中英

Yelp API搜索在Android調試版本中有效,但在發行版本中無效

[英]Yelp API search working in Android debug version but not in release version

我的Android應用使用Yelp搜索API。 當我在手機上安裝調試版本時,它可以正常工作,但是當我構建簽名的發行版本時,yelp api調用拒絕配合。 在實際的應用程序中,當我單擊foodButton,studyButton或otherButton時,將調用搜索功能,但絕對無法完成搜索功能。 我的假設是

while(!searchDone); 

是罪魁禍首,但我不知道為什么。

public class SexyMapFragment extends FragmentActivity implements GoogleMap.OnInfoWindowLongClickListener,GoogleMap.OnInfoWindowClickListener, OnMapReadyCallback {
    public static boolean backButton = false;
    OnMapReadyCallback mapListener = this;
    ArrayList<Business> businesses = new ArrayList<Business>();
    ArrayList<String> businessNames = new ArrayList<String>();
    ArrayList<String> textPhones = new ArrayList<String>();
    YelpAPIFactory apiFactory = new YelpAPIFactory("######", "######", "#####", "#####");
    YelpAPI yelpAPI = apiFactory.createAPI();
    String curTerm = "";
    boolean searchDone = false;
    double topY;
    double bottomY;
    double leftX;
    double rightX;
    boolean dismissButton = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sexy_map);
        MobileAds.initialize(getApplicationContext(), "########");
        AdView mAdView = (AdView) findViewById(R.id.adMap);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        leftX = Integer.MAX_VALUE;
        rightX = Integer.MIN_VALUE;
        topY = Integer.MIN_VALUE;
        bottomY = Integer.MAX_VALUE;
        for (String name : MainActivity.checked) {
            int index = in(MainActivity.friends,name);
            LatLng ltlng = MainActivity.latlongs.get(index);
            if (ltlng.latitude > topY) {
                topY = ltlng.latitude;
            }
            if (ltlng.latitude < bottomY) {
                bottomY = ltlng.latitude;
            }
            if (ltlng.longitude < leftX) {
                leftX = ltlng.longitude;
            }
            if (ltlng.longitude > rightX) {
                rightX = ltlng.longitude;
            }
        }
        MainActivity.midLat = (topY + bottomY) / 2.0;
        MainActivity.midLong = (leftX + rightX) / 2.0;
        // Inflate the layout for this fragment
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        findViewById(R.id.backMapButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                backButton = true;
                MainActivity.friends.clear();
                MainActivity.userPhones.clear();
                MainActivity.checked.clear();
                MainActivity.latlongs.clear();
                MainActivity.newUsers.clear();
                MainActivity.pending.clear();
                MainActivity.newUsers.clear();
                startActivity(new Intent(getApplicationContext(),MainActivity.class));
            }
        });
        findViewById(R.id.foodButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                curTerm = "food";
                SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                mapFragment.getMapAsync(mapListener);
            }
        });
        findViewById(R.id.studyButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                curTerm = "study";
                SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                mapFragment.getMapAsync(mapListener);
            }
        });
        findViewById(R.id.otherButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                findViewById(R.id.sexyMapLayout).setBackgroundColor(Color.rgb(60, 60, 60));
                findViewById(R.id.backMapButton).setAlpha(.1f);
                findViewById(R.id.backMapButton).setClickable(false);
                findViewById(R.id.foodButton).setAlpha(.1f);
                findViewById(R.id.foodButton).setClickable(false);
                findViewById(R.id.studyButton).setAlpha(.1f);
                findViewById(R.id.studyButton).setClickable(false);
                findViewById(R.id.otherButton).setAlpha(.1f);
                findViewById(R.id.otherButton).setClickable(false);
                LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View tempView = layoutInflater.inflate(R.layout.search_map_popup, null);
                final PopupWindow window = new PopupWindow(tempView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                window.setFocusable(true);
                window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        if (!dismissButton) {
                            window.setFocusable(true);
                            window.showAtLocation(tempView, Gravity.CENTER, 0, 0);
                        }
                        dismissButton = false;
                    }
                });
                window.showAtLocation(tempView, Gravity.CENTER, 0, 0);
                tempView.findViewById(R.id.popupCancelSearch).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        findViewById(R.id.sexyMapLayout).setBackgroundColor(Color.rgb(255, 255, 255));
                        findViewById(R.id.backMapButton).setAlpha(1f);
                        findViewById(R.id.backMapButton).setClickable(true);
                        findViewById(R.id.foodButton).setAlpha(1f);
                        findViewById(R.id.foodButton).setClickable(true);
                        findViewById(R.id.studyButton).setAlpha(1f);
                        findViewById(R.id.studyButton).setClickable(true);
                        findViewById(R.id.otherButton).setAlpha(1f);
                        findViewById(R.id.otherButton).setClickable(true);
                        dismissButton = true;
                        window.dismiss();
                    }
                });
                tempView.findViewById(R.id.popupDoSearch).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        curTerm = ((EditText)tempView.findViewById(R.id.popupSearchTerm)).getText().toString();
                        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                        mapFragment.getMapAsync(mapListener);
                        findViewById(R.id.sexyMapLayout).setBackgroundColor(Color.rgb(255, 255, 255));
                        findViewById(R.id.backMapButton).setAlpha(1f);
                        findViewById(R.id.backMapButton).setClickable(true);
                        findViewById(R.id.foodButton).setAlpha(1f);
                        findViewById(R.id.foodButton).setClickable(true);
                        findViewById(R.id.studyButton).setAlpha(1f);
                        findViewById(R.id.studyButton).setClickable(true);
                        findViewById(R.id.otherButton).setAlpha(1f);
                        findViewById(R.id.otherButton).setClickable(true);
                        dismissButton = true;
                        window.dismiss();
                    }
                });
            }
        });
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        googleMap.clear();
        googleMap.setOnInfoWindowClickListener(this);
        for(String name : MainActivity.checked) {
            int index = in(MainActivity.friends,name);
            String curPhone = MainActivity.userPhones.get(index);
            textPhones.add(curPhone);
            googleMap.addMarker(new MarkerOptions().position(MainActivity.latlongs.get(index)).title(name).snippet(curPhone));
        }
        googleMap.addMarker(new MarkerOptions().position(new LatLng(MainActivity.midLat, MainActivity.midLong)).title("MIDPOINT").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))));
        if(!curTerm.equals("")) {
            businesses.clear();
            search(curTerm);
            while (!searchDone) ;
            searchDone = false;
            for (Business business : businesses) {
                googleMap.addMarker(new MarkerOptions().position(new LatLng(business.location().coordinate().latitude(), business.location().coordinate().longitude())).title(business.name()).snippet(getAddressFromName(business.name())).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
            }
        }
        googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(new LatLngBounds(new LatLng(bottomY,leftX),new LatLng(topY,rightX)),0));
        googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }
            @Override
            public View getInfoContents(Marker marker) {
                View myContentView = null;
                if(in(businessNames,marker.getTitle())!=-1) {
                    myContentView = getLayoutInflater().inflate(
                        R.layout.info_item, null);
                    TextView tvTitle = ((TextView) myContentView
                        .findViewById(R.id.bizNameText));
                    TextView tvSnippet = ((TextView) myContentView
                        .findViewById(R.id.snippetText));
                    tvTitle.setText(marker.getTitle());
                    tvSnippet.setText(marker.getSnippet());
                }
                else  {
                    myContentView = getLayoutInflater().inflate(R.layout.classic_item,null);
                    ((TextView)myContentView.findViewById(R.id.markerTitleWin)).setText(marker.getTitle());
                    ((TextView)myContentView.findViewById(R.id.markerSnippetWin)).setText(marker.getSnippet());
                }
                return myContentView;
            }
        });
        googleMap.setOnInfoWindowLongClickListener(this);
    }

    public void search(final String term) {
        new Thread() {
            public void run() {
                Map<String, String> params = new HashMap<>();
                params.put("term",term);
                params.put("limit", "10");
                CoordinateOptions coordinate = CoordinateOptions.builder().latitude(MainActivity.midLat).longitude(MainActivity.midLong).build();
                Call<SearchResponse> call = yelpAPI.search(coordinate, params);
                Response<SearchResponse> response = null;
                try {
                    response = call.execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(response==null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"Nothing within 25 miles! Is your midpoint in the middle of the ocean?",Toast.LENGTH_LONG).show();
                        }
                    });
                    searchDone = true;
                    return;
                }
                ArrayList<Business> myBusinesses = response.body().businesses();
                businessNames = new ArrayList<String>();
                for(Business business : myBusinesses) {
                    businesses.add(business);
                    businessNames.add(business.name());
                }
                searchDone=true;
            }
        }.start();
    }
    public int in(ArrayList<String> arr, String check) {
        for(int i = 0; i < arr.size(); i++) {
            if(arr.get(i).equals(check)) {
                return i;
            }
        }
        return -1;
    }
}

我衷心為松散的代碼表示歉意。 所有這些都必須非常快地完成,因此質量不是相當的stackoverflow層,但是我希望有人可以提供幫助。

終於想通了。 我想在調試版本中,Android使您在UI線程上有更多自由來運行我所做的更改:

while(!searchDone);

while(!searchDone) {
    try {
        Thread.sleep(25);
    }catch(Exception e){e.printStackTrace();}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM