iT邦幫忙

0

如何使用StringRequest和JSONObject 達成傳遞一值給php連mysql 同時傳data回來

我目前在做個map app 其中一個方法是要將用戶自訂的marker顯示出來

在測試階段先用getUserMarkers()取得資料

private void getUserMarkers() {

        StringRequest strReQ = new StringRequest(Request.Method.POST,userurl ,new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("Response: ", response.toString());

                try {
                    JSONObject jObj = new JSONObject(response);
                    String getObject = jObj.getString("node");
                    JSONArray jsonArray = new JSONArray(getObject);

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        title = jsonObject.getString(TITLE);
                        address = jsonObject.getString(ADDRESS);
                        phone_number = jsonObject.getString(PHONE_NUMBER);
                        star = Double.parseDouble(jsonObject.getString(STAR));
                        latLng = new LatLng(Double.parseDouble(jsonObject.getString(LAT)), Double.parseDouble(jsonObject.getString(LNG)));

                        addUserMarker(latLng, title,phone_number,address,star);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Error: ", error.getMessage());
                Toast.makeText(MapActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
        AppController.getInstance().addToRequestQueue(strReQ, tag_json_obj);
    }

再用addUserMarker()建立marker

private void addUserMarker(LatLng latlng, final String title,final String phone_number,final String address,final Double star) {

        mSnippet ="地址: "+address+"\n"
                +"電話: "+phone_number+"\n"
                +"評價: "+star;

        markerOptionS.position(latlng)
                .title(title)
                .snippet(mSnippet)
                .icon(defaultMarker(BitmapDescriptorFactory.HUE_BLUE));

        Usermarker = mMap.addMarker(markerOptionS);
        Usermarker.setTag("1");

        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Toast.makeText(getApplicationContext(), marker.getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }

成功呼叫了php連到mysql並傳回資料建立了marker

但我現在要傳遞一個值來搜尋資料庫

比如 user_id="0001", 傳遞user_id來取回"0001"建立的marker

請問該怎麼做 麻煩了

尚未有邦友回答

立即登入回答