如何使用Codeignitor将数据从android活动发布到mysql数据库中?

时间:2019-05-06 16:23:16

标签: android codeigniter

我想实现搜索活动,其中输入将从编辑文本中获取,并通过网络视图进行搜索!

codeignitor控制器搜索功能

 public function studentlistview(){
    if($this->input->post('search')!=""){
        $r=$this->studentmodel->search();
    }
        else{
    $r=$this->studentmodel->show();
    }
    $w=array('row'=>$r);
    $this->load->view('studentlistview',$w);

}
}

Android Studio搜索

     public void search(){


    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String search=textView.getText().toString();
            BackgroundWorker backgroundWorker=new BackgroundWorker(this);
            backgroundWorker.execute(search);
        }
    });
 }

class BackgroundWorker extends AsyncTask<String,Void,String>{

Context context;
public BackgroundWorker(View.OnClickListener ctx){
    context= (Context) ctx;
}

@Override
protected String doInBackground(String... params) {
    String search=params[0];
    String listurl="https://abcd.com";
    try {
        URL url=new URL(listurl);
        HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        OutputStream outputStream=httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
        String post_data= URLEncoder.encode("search","UTF-8")+"="+URLEncoder.encode(search,"UTF-8");
        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        InputStream inputStream=httpURLConnection.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
        String result="";
        String line="";
        while((line=bufferedReader.readLine())!=null){
            result+=line;
        }
        bufferedReader.close();
        inputStream.close();
        httpURLConnection.disconnect();
        return result;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    context.startActivity(new Intent(context,issueweb.class));
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

codeignitor模型搜索功能

      public function search(){


    $s=$this->input->post("search");
    $this->db->where("book LIKE '%$s%' OR author LIKE '%$s%' OR topic LIKE '%$s%'");
    $st=$this->db->get('book');
    return $ret=$st->result();
}

postexecute方法没有以某种方式实现,再次使我回到上一个活动!

1 个答案:

答案 0 :(得分:0)

使用$this->input->post()代替$this->post()

public function search(){


    $s= $this->post('search');
    $this->db->where("book LIKE '%$s%' OR author LIKE '%$s%' OR topic LIKE '%$s%'");
    $st=$this->db->get('book');
    return $ret=$st->result();
}