Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s = {"result":[{"post_id":"390","post_title":"Cart","post_date":"2017-02-07 12:17:29"},{"post_id":"421","post_title":"Front End Developer - Digital Arts","post_date":"2017-02-07 12:18:04"},{"post_id":"431","post_title":"Art Director","post_date":"2017-02-07 12:18:19"}]}
- public class Parser extends AsyncTask <Void, Void,Integer> {
- Context ctx;
- ListView listView;
- String data;
- ArrayList<Skill> skills = new ArrayList<>();
- ArrayAdapter adapter;
- 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) {
- adapter = new ArrayAdapter(ctx, android.R.layout.simple_list_item_1, titles);
- // CustomAdapter mAdapter = new CustomAdapter(ctx,skills);
- adapter.notifyDataSetChanged();
- listView.setAdapter(adapter);
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Intent toSkillActivity = new Intent(ctx,SkillActivity.class);
- toSkillActivity.putExtra("skill",skills.get(position));
- ctx.startActivity(toSkillActivity);
- }});
- } else {
- Toast.makeText(ctx, "Unable to Parse", Toast.LENGTH_LONG).show();
- }
- }
- private int parse() {
- try {
- Log.d("Jou","data");
- JSONArray ja = new JSONArray(data);
- JSONObject jo = null;
- titles.clear();
- skills.clear();
- for (int i = 0; i < ja.length(); i++) {
- jo = ja.getJSONObject(i);
- String author = jo.getString("post_author");
- String title = jo.getString("post_title");
- //String content = jo.getString("post_content");/* authoer is better :P */
- String date = jo.getString("post_date");
- Skill skill = new Skill();
- skill.setAuthor(author);
- skill.setTitle(title);
- //skill.setContent(content);
- skill.setDate(date);
- skills.add(skill);
- titles.add(title);
- }
- return 1;
- } catch (JSONException e) {
- Log.d("Jou", e.getMessage());
- return 0;
- }
- }}
- <?php
- $dbhost = 'localhost';
- $dbuser = '';
- $dbpass = '';
- $conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect") ;
- if(! $conn )
- {
- echo 'Could not connect: ' . mysqli_error();
- }
- error_reporting(-1);
- ini_set('display_errors', 'On');
- mysqli_set_charset($conn, 'utf8');
- $search ="";
- if(isset($_REQUEST['query'] )){
- $search = $_REQUEST['query'];
- }
- if($search != ""){
- $sql = "SELECT post_author,post_title,post_date FROM `wp_posts` WHERE post_title LIKE '%".$search."%'";
- mysqli_select_db($conn,'');
- $query = mysqli_query($conn, $sql ) or die ("Error: ".mysqli_error($conn));;
- $result = array();
- while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
- array_push($result,
- array('post_author'=>$row['post_author'],
- 'post_title'=>$row['post_title'],
- 'post_date'=>$row['post_date']
- ));
- }
- echo json_encode(array("result"=>$result));
- }else{
- echo 'No search field has been sent';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement