دریافت از $_REQUEST
سلام مشکل رو سریع میگم ... این کد سمت اندرویدم که یک اطلاعاتی رو میفرستم و همه چیزم به خوبی کار میکنه فقط وقتی اطلاعاتی رو میفرستم که قراره توی یک تیکه کدی که like داره استفاده شه واسه جستجو کردن کار نمیکنه و انگار request مقدار رشته ای رو داخل متغیرم نمیریزه چون از همه نظر چک کردم درست بود دستور اس کیو ال بنده ... کد رو میزارم ساده و کم یک نگاه بکنین راهنمایی کنیم
خودم حس میکنم از ذات request مشکل باشه ولی در جای دیگم دیدم توی پستهای همین سایت که طرف به عنوان نمونه دریافت کرده و حتی توی like هم استفاده کرده ولی دیکه نمیدونم چقدر درست بوده اون کد یا نه
کد سمت اندروید :
G.params.add(new BasicNameValuePair("user_id", G.cellcode));
G.params.add(new BasicNameValuePair("word", input));
Commands.search(G.params);
--------------------------------------------------
public static void search(ArrayList<NameValuePair> params) {
G.getJsonString = Webservice.readUrl(url + "search", params);
}
این هم کدهای سمت سرور از الان بگم که در این قسمت متغیر $word رو حتی توی '' قراردادم ولی جواب نمیده لطفا راهنمایی کنین خط مشکل ساز رو هم مشخص کردم توی کد
if ($action=="search"){
search();
}
----------------------------
function search(){
$connection = connectToDatabase();
$user_id = $_REQUEST['user_id'];
$word = $_REQUEST['word'];
$output = array();
$record = array();
خط مشکل ساز -> $result = mysqli_query($connection, "SELECT * FROM news WHERE news_title like '%$word%'");
while($row = mysqli_fetch_array($result)) {
$record['news_id'] = $row['news_id'];
$record['news_title'] = $row['news_title'];
$record['page_number'] = $row['page_number'];
$record['url'] = $row['url'];
$output[] = $record;
}
echo json_encode($output);
mysqli_close($connection);
}
دقیقآ نمیشه فهمید مشکل از کجاست و کدهات هم مشکلی نداره
این کد رو جایگزین اون قسمتی که گفتی مشکل داره کن:
$word = '%'.mysqli_real_escape_string($connection,$word).'%';
$result = mysqli_query($connection,"SELECT * FROM news WHERE news_title like '$word' ");
public static String url = "http://192.168.1.4/learneng2/service.php?action=";
G.params.add(new BasicNameValuePair("user_id", G.cellcode));
G.params.add(new BasicNameValuePair("word_s", input));
Commands.search(G.params);
public static void search(ArrayList<NameValuePair> params) {
G.getJsonString = Webservice.readUrl(url + "search", params);
}
کد کامل سمت آندروید رو نوشتم
package mirian.learnenglish2.fin;
public class ActivitySearch extends ActivityCheck {
private String input;
static ListView lstContent = new ListView(G.context);
public static AsyncTask<Void, Void, Void> task;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
activityMenu = new CustomizedSlidingMenu(this);
ViewGroup box = (ViewGroup) findViewById(R.id.box);
G.searchbool = true;
//------------------------ set list view
lstContent = (ListView) findViewById(R.id.LstContent);
G.adapterfav = new AdapterFav(G.notes);
lstContent.setAdapter(G.adapterfav);
G.notes.clear();
// SearchFunction();
final View searchbt = box.findViewById(R.id.srcbt);
final View searchbox = box.findViewById(R.id.srcbox);
findViewById(R.id.mother).requestFocus();
//----
searchbt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
input = ((TextView) searchbox).getText().toString();
((TextView) searchbox).setText("");
// set keyboard hide
if ( !input.contentEquals(""))
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchbox.getWindowToken(), 0);
// set keyboard hide
SearchFunction();
}
}
});
//----
}
private void SearchFunction() {
task = new AsyncTask<Void, Void, Void>() {
ProgressDialog dialog = new ProgressDialog(ActivitySearch.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
lstContent = (ListView) findViewById(R.id.LstContent);
G.adapterfav = new AdapterFav(G.notes);
lstContent.setAdapter(G.adapterfav);
//lstContent.setOnScrollListener(new OnScrollListener
dialog.setMessage("درحال بارگزاری");
dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// if (G.notes.size() < 1) {
// params.add(new BasicNameValuePair("part", "" + part));
Log.i(G.LOG_TAG, "search " + input);
G.params.add(new BasicNameValuePair("user_id", G.cellcode));
G.params.add(new BasicNameValuePair("word", input));
Commands.search(G.params);
// }
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
LoadMore();
}
}
};
task.execute((Void) null);
}
public static void LoadMore() {
if (G.getJsonString != null) {
try {
Log.i(G.LOG_TAG, "GGG " + G.getJsonString);
G.notes.clear();
JSONArray jsonnews = new JSONArray(G.getJsonString);
Log.i(G.LOG_TAG, "length of json " + jsonnews.length());
G.jsonSize = jsonnews.length();
for (int j = 0; j < jsonnews.length(); j++) {
JSONObject object = jsonnews.getJSONObject(j);
StructNews note = new StructNews();
note.id = object.getInt("news_id");
note.pagenumber = object.getInt("page_number");
note.Title = object.getString("news_title");
// note.isfav = object.getInt("is_fav");
note.url = object.getString("url");
// note.description = object.getString("news_desc");
Log.i(G.LOG_TAG, "sss " + object.getString("news_title"));
G.notes.add(note);
}
G.adapterfav.notifyDataSetChanged();
task.cancel(true);
Log.i(G.LOG_TAG, "task is " + task.getStatus());
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
protected void onPause() {
super.onPause();
if (activityMenu.isMenuShowing())
activityMenu.toggle();
if (task != null)
task.cancel(true);
}
@Override
protected void onResume() {
super.onResume();
G.searchbool = true;
// SearchFunction();
lstContent = (ListView) findViewById(R.id.LstContent);
G.adapterfav.notifyDataSetChanged(); // اطلاعات لیست ویو آپدیت میشود .
//CustomizedSlidingMenu menu = new CustomizedSlidingMenu(this); // ایجاد منوی کناری
}
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .