使用齐射搜索MySQL数据库

时间:2014-10-17 10:23:21

标签: php android mysql

我尝试使用php从MySQL数据库中检索所有数据,并且在我的应用程序中这样做是成功的。现在我尝试使用edittext将该字符串传递给php,但我无法这样做。我很成功,但没有使用凌空图书馆。有没有办法添加"关键字"在url使用凌空之后,因为这是我需要的唯一工作吗? 在之前的版本中,我使用JSONObject json = jParser.makeHttpRequest(url_search, "GET", params);将params传递给php ..

示例和代码: 我希望将其作为url字符串传递:http://myip/myproject/search.php?keyword .

我通常使用此代码: 在edittext字段上 - > searchIntent.putExtra("关键字",txtkeyword.getText()的toString());

在我的搜索活动中:

public class AllEmpresasActivity extends Activity {
    // Log tag
    private static final String TAG = AllEmpresasActivity.class.getSimpleName();

    // Empresas json url
    private static final String url = "http://192.168.1.90/android_connect/search.php?params=%1$s,num1";

    private ProgressDialog pDialog;
    private List<Empresa> empresaList = new ArrayList<Empresa>();
    private ListView listView;
    private CustomListAdapter adapter;

    public String searchkey;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_empresas);

        Intent searchIntent = getIntent();
        searchkey = searchIntent.getStringExtra("keyword");

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, empresaList);
        listView.setAdapter(adapter);

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();

        // changing action bar color
        getActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#1b1b1b")));

        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Empresa empresa = new Empresa();
                                empresa.setMarca(obj.getString("marca"));
                                empresa.setThumbnailUrl(obj.getString("imagempequena"));
                                empresa.setMarcatotal(obj.getString("marcatotal"));
                                empresa.setDatainicio(obj.getString("datainicio"));
                                empresa.setActividade(obj.getString("actividade"));

                                // adding empresa to empresa array
                                empresaList.add(empresa);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

}

2 个答案:

答案 0 :(得分:1)

public class AllEmpresasActivity extends Activity {
    ...
    private static final String sUrl = "http://myip/myproject/search.php?keyword=%1$s";
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        String searchKey = getIntent().getStringExtra("keyword");
        String url = String.format(sUrl, searchKey);
        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        ...

答案 1 :(得分:0)

根据我从你理解的内容我修改了我的代码如下。

private static final String url = "http://****/get_items.php?keyword=%1$s";
String searchKey = getIntent().getStringExtra("2");
        Final_url = String.format(url, searchKey);

当我像这样打印Final_url时

 Toast.makeText(getApplicationContext(),"url "+ Final_url, Toast.LENGTH_SHORT).show();

打印值始终为

http://*****/get_items.php?keyword=null

我有什么想念吗?