Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. 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"}]}
  2.  
  3. public class Parser extends AsyncTask <Void, Void,Integer> {
  4.  
  5. Context ctx;
  6. ListView listView;
  7. String data;
  8. ArrayList<Skill> skills = new ArrayList<>();
  9. ArrayAdapter adapter;
  10. ArrayList<String> titles = new ArrayList<>();
  11.  
  12. public Parser(Context ctx, String data, ListView listView) {
  13. this.ctx = ctx;
  14. this.data=data;
  15. this.listView = listView;
  16. }
  17.  
  18. @Override
  19. protected void onPreExecute() {
  20. super.onPreExecute();
  21. }
  22.  
  23. @Override
  24. protected Integer doInBackground(Void... params) {
  25. return this.parse();
  26. }
  27.  
  28. @Override
  29. protected void onPostExecute(Integer integer) {
  30. super.onPostExecute(integer);
  31. if (integer == 1) {
  32. adapter = new ArrayAdapter(ctx, android.R.layout.simple_list_item_1, titles);
  33. // CustomAdapter mAdapter = new CustomAdapter(ctx,skills);
  34. adapter.notifyDataSetChanged();
  35. listView.setAdapter(adapter);
  36. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  37. @Override
  38. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  39. Intent toSkillActivity = new Intent(ctx,SkillActivity.class);
  40. toSkillActivity.putExtra("skill",skills.get(position));
  41. ctx.startActivity(toSkillActivity);
  42. }});
  43. } else {
  44. Toast.makeText(ctx, "Unable to Parse", Toast.LENGTH_LONG).show();
  45. }
  46. }
  47.  
  48.  
  49. private int parse() {
  50. try {
  51. Log.d("Jou","data");
  52. JSONArray ja = new JSONArray(data);
  53. JSONObject jo = null;
  54. titles.clear();
  55. skills.clear();
  56. for (int i = 0; i < ja.length(); i++) {
  57. jo = ja.getJSONObject(i);
  58. String author = jo.getString("post_author");
  59. String title = jo.getString("post_title");
  60. //String content = jo.getString("post_content");/* authoer is better :P */
  61. String date = jo.getString("post_date");
  62. Skill skill = new Skill();
  63. skill.setAuthor(author);
  64. skill.setTitle(title);
  65. //skill.setContent(content);
  66. skill.setDate(date);
  67. skills.add(skill);
  68. titles.add(title);
  69. }
  70. return 1;
  71. } catch (JSONException e) {
  72. Log.d("Jou", e.getMessage());
  73. return 0;
  74. }
  75. }}
  76.  
  77. <?php
  78. $dbhost = 'localhost';
  79. $dbuser = '';
  80. $dbpass = '';
  81. $conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect") ;
  82. if(! $conn )
  83. {
  84. echo 'Could not connect: ' . mysqli_error();
  85. }
  86. error_reporting(-1);
  87. ini_set('display_errors', 'On');
  88. mysqli_set_charset($conn, 'utf8');
  89.  
  90. $search ="";
  91. if(isset($_REQUEST['query'] )){
  92. $search = $_REQUEST['query'];
  93. }
  94. if($search != ""){
  95.  
  96. $sql = "SELECT post_author,post_title,post_date FROM `wp_posts` WHERE post_title LIKE '%".$search."%'";
  97.  
  98. mysqli_select_db($conn,'');
  99.  
  100. $query = mysqli_query($conn, $sql ) or die ("Error: ".mysqli_error($conn));;
  101. $result = array();
  102. while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
  103.  
  104. array_push($result,
  105. array('post_author'=>$row['post_author'],
  106. 'post_title'=>$row['post_title'],
  107. 'post_date'=>$row['post_date']
  108. ));
  109. }
  110. echo json_encode(array("result"=>$result));
  111. }else{
  112. echo 'No search field has been sent';
  113. }
  114.  
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement