Yelp API搜索在Android调试版中工作,但在发行版中没有

时间:2016-08-07 06:14:11

标签: java android google-maps yelp

我的Android应用使用Yelp搜索API。当我在手机上安装调试版本时,它工作正常,但是当我构建签名版本时,yelp api调用拒绝合作。在实际的应用程序中,当我单击foodButton,studyButton或其他按钮时,搜索函数会被调用,但它肯定无法完成。

我的假设是
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-tier,但我希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:0)

终于明白了。我想在调试版本中,Android可以让你在UI线程上更自由地运行我改变的任何内容:

while(!searchDone);

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