我正在开发一个搜索功能(搜索MySQL数据库中的表),但它无法正常工作。每当我在搜索栏中输入一个值时,吐司显示"搜索然后不显示结果。我有setOnQueryTextListener存在的主要活动。
public class FindSkill extends AppCompatActivity {
SearchView searchView;
ListView listView;
ImageView noData, noNetwork;
String urlAdress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.find_skill);
listView = (ListView) findViewById(R.id.searchList);
searchView = (SearchView) findViewById(R.id.searchView);
noData = (ImageView) findViewById(R.id.nodata);
noNetwork = (ImageView) findViewById(R.id.nonetwork);
urlAdress = "http://skillsexchangecyprus.com/SEC/ss.php";
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
SenderReceiver sr = new SenderReceiver(FindSkill.this, urlAdress,listView, query,noData,noNetwork);
sr.execute();
return false;
}
@Override
public boolean onQueryTextChange(String query) {
SenderReceiver sr = new SenderReceiver(FindSkill.this, urlAdress, listView, query, noData, noNetwork);
sr.execute();
return false;
}});}
}
这是SenderReceiver的代码:
public class SenderReceiver extends AsyncTask<Void,Void,String>{
Context ctx;
String urlAdress;
String query;
ListView listView;
ImageView noData, noNetwork;
ProgressDialog progressDialog;
public SenderReceiver(Context ctx, String urlAdress, ListView listView, String query, ImageView... imageViews){
this.ctx=ctx;
this.urlAdress=urlAdress;
this.listView=listView;
this.query=query;
this.noData=imageViews[0];
this.noNetwork=imageViews[1];
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=new ProgressDialog(ctx);
progressDialog.setTitle("Search");
progressDialog.setMessage("Searching... Please Wait!");
progressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
return this.sendAndReceive();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
//Reset listView
listView.setAdapter(null);
if ( s!= null ){
if(! s.contains("null")) {
if(s.length() > 0){
Parser parser = new Parser(ctx,s,listView);
parser.execute();
}
}else {
noNetwork.setVisibility(View.INVISIBLE);
noData.setVisibility(View.VISIBLE);
} //data = "[{\"post_title\":\"title1\"},{\"post_title\":\"title2\"}]";
}else {
noNetwork.setVisibility(View.INVISIBLE);
noData.setVisibility(View.VISIBLE);
}
}
private String sendAndReceive(){
HttpURLConnection connection = Connector.connection(urlAdress);
if (connection == null){
return null;
}
try {
String urlParameters = "query=art";
connection.setDoOutput(true);
OutputStreamWriter outputStream = new OutputStreamWriter(connection.getOutputStream());
outputStream.write(urlParameters);
outputStream.flush();
// OutputStream outputStream = connection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter((outputStream));// write it to the network
bufferedWriter.write(new DataBackager(query).packageData());
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
//response
int responseCode = connection.getResponseCode();
//decode
if (responseCode == connection.HTTP_OK){
//return data
InputStream inputStream = connection.getInputStream();
//read data
BufferedReader bufferedReader = new BufferedReader(( new InputStreamReader(inputStream)));
String line;
StringBuffer response = new StringBuffer();
if (bufferedReader != null){
while ((line= bufferedReader.readLine()) != null)
{
response.append(line + "\n");
}
}else {
return null;
}
return response.toString();
}else{
return String.valueOf(responseCode);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
在搜索栏中输入日志时显示的内容:
03-22 20:19:10.881 27300-27300/com.example.joudialfattal.skillsexchange D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{8d1f108 V.E...... R.....I. 0,0-0,0}
03-22 20:19:10.891 27300-28889/com.example.joudialfattal.skillsexchange I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-22 20:19:10.891 27300-28889/com.example.joudialfattal.skillsexchange I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-22 20:19:10.891 27300-27300/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-22 20:19:10.941 27300-27456/com.example.joudialfattal.skillsexchange D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [1592x909]-format:1
03-22 20:19:10.961 27300-27300/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-22 20:19:10.991 27300-27300/com.example.joudialfattal.skillsexchange D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
03-22 20:19:11.671 27300-27300/com.example.joudialfattal.skillsexchange D/ViewRootImpl: #3 mView = null
03-22 20:19:11.701 27300-27300/com.example.joudialfattal.skillsexchange E/ViewRootImpl: sendUserActionEvent() mView == null
03-22 20:19:15.261 27300-27300/com.example.joudialfattal.skillsexchange D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{89ca702 V.E...... R.....I. 0,0-0,0}
03-22 20:19:15.271 27300-28989/com.example.joudialfattal.skillsexchange I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-22 20:19:15.271 27300-28989/com.example.joudialfattal.skillsexchange I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-22 20:19:15.281 27300-27300/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-22 20:19:15.331 27300-27456/com.example.joudialfattal.skillsexchange D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [1592x909]-format:1
03-22 20:19:15.341 27300-27300/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-22 20:19:15.351 27300-27300/com.example.joudialfattal.skillsexchange D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
03-22 20:19:15.451 27300-27300/com.example.joudialfattal.skillsexchange D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
03-22 20:19:15.481 27300-27300/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-22 20:19:15.991 27300-27300/com.example.joudialfattal.skillsexchange D/ViewRootImpl: #3 mView = null
03-22 20:19:16.031 27300-27300/com.example.joudialfattal.skillsexchange E/ViewRootImpl: sendUserActionEvent() mView == null
03-22 20:19:16.151 27300-27300/com.example.joudialfattal.skillsexchange D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 96 - 0, 1128) vi=Rect(0, 96 - 0, 1128) or=1
03-22 20:19:16.161 27300-27300/com.example.joudialfattal.skillsexchange W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-22 20:19:45.241 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection
03-22 20:19:45.431 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection
03-22 20:19:45.531 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
03-22 20:19:45.531 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
03-22 20:19:45.531 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
03-22 20:19:45.671 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
03-22 20:19:45.691 27300-27300/com.example.joudialfattal.skillsexchange V/ActivityThread: updateVisibility : ActivityRecord{77cc7ba token=android.os.BinderProxy@39d6639 {com.example.joudialfattal.skillsexchange/com.example.joudialfattal.skillsexchange.FindSkill}} show : true
03-22 20:19:45.691 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
03-22 20:19:45.701 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
03-22 20:19:45.701 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
03-22 20:19:45.711 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
03-22 20:19:45.711 27300-27300/com.example.joudialfattal.skillsexchange W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
03-22 20:19:46.101 27300-27300/com.example.joudialfattal.skillsexchange D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
Pasrer类代码:
public class Parser extends AsyncTask <Void, Void,Integer> {
Context ctx;
ListView listView;
String data;
ArrayList<String> titles = new ArrayList<>();
public Parser(Context ctx, String data, ListView listView) {
this.ctx = ctx;
this.data=data;
this.listView = listView;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Integer doInBackground(Void... params) {
return this.parse();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if (integer == 1) {
ArrayAdapter adapter = new ArrayAdapter(ctx, android.R.layout.simple_list_item_1, titles);
listView.setAdapter(adapter);
} else {
Toast.makeText(ctx, "Unable to Parse", Toast.LENGTH_LONG).show();
}
}
private int parse() {
try {
JSONArray ja = new JSONArray(data);
JSONObject jo = null;
titles.clear();
for (int i = 0; i < ja.length(); i++) {
jo = ja.getJSONObject(i);
String title = jo.getString("post_title");
titles.add(title);
}
return 1;
} catch (JSONException e) {
return 0;
}
}
}
PS:PHP代码100%正常工作,因为我尝试使用数据库并显示了所需的结果。
答案 0 :(得分:0)
尝试更改
urlAdress = "http://skillsexchangecyprus.com/SEC/ss.php";
到
urlAdress = "https://skillsexchangecyprus.com/SEC/ss.php";