gpt4 book ai didi

php - 使用 volley 搜索 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 00:08:23 25 4
gpt4 key购买 nike

我尝试使用 php 从 MySQL 数据库中检索所有数据,并在我的应用程序中成功地这样做了。现在我尝试使用 edittext 将该字符串传递给 php,但我无法这样做。我成功了,但没有使用 volley 库。有什么方法可以使用 volley 在 url 之后添加“关键字”,因为这是我唯一需要的东西吗?在以前的版本中,我使用 JSONObject json = jParser.makeHttpRequest(url_search, "GET", params); 将参数传递给 php..

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

我正在使用这段代码,就像往常一样:在 edittext 字段上 -> searchIntent.putExtra("keyword",txtkeyword.getText().toString());

在我的搜索 Activity 中:

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;
}
}

}

最佳答案

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>() {
...

关于php - 使用 volley 搜索 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26422959/

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