search.xmlsearch.javaApiService.javasearch.php+
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_weight="0.07"
android:background="@color/colorPrimary">
<EditText
android:layout_width="0dip"
android:layout_height="match_parent"
android:id="@+id/edt_search"
android:layout_weight="0.4"
android:textColor="#fc3131"/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:text="جستجو"
android:id="@+id/btn_search"
android:background="@color/colorPrimary"
android:textSize="18dp"
android:textColor="#fff"
android:layout_weight="0.2"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
public class search extends AppCompatActivity {
EditText edt_search;
private final int REQ_CODE_SPEECH_INPUT = 100;
private RecyclerView recyclerView;
private MyRecyclerViewAdapter adapter;
private ApiService apiService;
String searchtxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
edt_search = (EditText) findViewById(R.id.edt_search);
final Button btn_search = (Button) findViewById(R.id.btn_search);
btn_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchtxt = edt_search.getText().toString();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(search.this,
LinearLayoutManager.VERTICAL, false));
apiService = new ApiService(search.this);
apiService.getData1(searchtxt, new ApiService.onGetData() {
@Override
public void onGet(List<NewsFeed> feeds) {
adapter = new MyRecyclerViewAdapter(feeds, search.this);
recyclerView.setAdapter(adapter);
}
});
}
});
}
public void getData1(String searchtxt,final onGetData onGetData) {
MainActivity.showDialog();
String encodedString = null;
try {
encodedString = URLEncoder.encode(searchtxt, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String url = " http://localhost/search.php?search=" + encodedString;
final JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url,
null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, "onResponse: " + response.toString());
List<NewsFeed> feeds = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
NewsFeed feed = new NewsFeed();
try {
JSONObject object = response.getJSONObject(i);
//مقدار هایی که میخاهید از سمت سرور بگیرین
feed.setImgUrl(object.getString("img"));
feed.setFeedName(object.getString("name"));
feed.setComment(object.getString("comment"));
} catch (JSONException e) {
e.printStackTrace();
}
}
onGetData.onGet(feeds);
search.hideDialog();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onGetData.onGet(null);
Log.e(TAG, "onErrorResponse: " + error.getMessage());
search.hideDialog();
String message = null;
if (error instanceof NetworkError) {
message = "نمی توان به اینترنت متصل شد لطفا دوباره امتحان کنید";
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
} else if (error instanceof AuthFailureError) {
message = "نمی توان به اینترنت متصل شد لطفا اتصال خود را بررسی نمایید";
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
} else if (error instanceof NoConnectionError) {
message = "نمی توان به اینترنت متصل شد لطفا اتصال خود را بررسی نمایید";
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
} else if (error instanceof TimeoutError) {
message = "ارتباط برقرار نشد لطفا دوباره سعی نمایید";
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}
});
request.setRetryPolicy(new DefaultRetryPolicy(8000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getmIstance().getmRequestQueue().add(request);
}
<?php
$con=mysqli_connect("localhost","user","pass","DB");
mysqli_set_charset($con,"utf8");
$s = $_REQUEST["search"];
$sql = "SELECT * FROM ghaza WHERE (name LIKE N'%$s%')";
$res = mysqli_query($con,$sql);
$result = array();
while ($row = mysqli_fetch_array($res)) {
$item = array();
$item['success']="1";
$item['id'] = $row[0];
$item['name'] = $row[1];
$item['price'] = $row[2];
$item['comment'] = $row[3];
$item['img'] = $row[4];
$item['iddaste'] = $row[5];
$item['takhfif'] = $row[6];
$result[] = $item;
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
mysqli_close($con);
?>