Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. $host="";
  2. $username="";
  3. $password="";
  4. $db_name="";
  5.  
  6. $con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
  7. mysql_select_db("$db_name")or die("cannot select DB");
  8. $sql = "select * from pedidos";
  9. $result = mysql_query($sql);
  10. $json = array();
  11.  
  12. if(mysql_num_rows($result)){
  13. while($row=mysql_fetch_assoc($result)){
  14. $json['users1'][]=$row;
  15. }
  16. }
  17. mysql_close($con);
  18. echo json_encode($json);
  19. ?>
  20.  
  21. private String jsonResult;
  22. private String url = "http://proyectocul.mipropia.com/jsondiego/pedidos.php";
  23. private ListView listView;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_verpedido);
  29. listView = (ListView) findViewById(R.id.listviewpedido);
  30. accessWebService();
  31. }
  32.  
  33. @Override
  34. public boolean onCreateOptionsMenu(Menu menu) {
  35. // Inflate the menu; this adds items to the action bar if it is present.
  36. getMenuInflater().inflate(R.menu.menu_main, menu);
  37. return true;
  38. }
  39.  
  40. // Async Task to access the web
  41. private class JsonReadTask extends AsyncTask<String, Void, String> {
  42. @Override
  43. protected String doInBackground(String... params) {
  44. HttpClient httpclient = new DefaultHttpClient();
  45. HttpPost httppost = new HttpPost(params[0]);
  46. try {
  47. HttpResponse response = httpclient.execute(httppost);
  48. jsonResult = inputStreamToString(
  49. response.getEntity().getContent()).toString();
  50. }
  51.  
  52. catch (ClientProtocolException e) {
  53. e.printStackTrace();
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. }
  57. return null;
  58. }
  59.  
  60. private StringBuilder inputStreamToString(InputStream is) {
  61. String rLine = "";
  62. StringBuilder answer = new StringBuilder();
  63. BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  64.  
  65. try {
  66. while ((rLine = rd.readLine()) != null) {
  67. answer.append(rLine);
  68. }
  69. }
  70.  
  71. catch (IOException e) {
  72. // e.printStackTrace();
  73. Toast.makeText(getApplicationContext(),
  74. "Error..." + e.toString(), Toast.LENGTH_LONG).show();
  75. }
  76. return answer;
  77. }
  78.  
  79. @Override
  80. protected void onPostExecute(String result) {
  81. ListDrwaer();
  82. }
  83. }// end async task
  84.  
  85. public void accessWebService() {
  86. JsonReadTask task = new JsonReadTask();
  87. // passes values for the urls string array
  88. task.execute(new String[]{url});
  89. }
  90.  
  91. // build hash set for list view
  92. public void ListDrwaer() {
  93. List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
  94.  
  95. try {
  96. JSONObject jsonResponse = new JSONObject(jsonResult);
  97. JSONArray jsonMainNode = jsonResponse.optJSONArray("users1");
  98.  
  99.  
  100. for (int i = 0; i < jsonMainNode.length(); i++) {
  101. JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
  102. String name = jsonChildNode.optString("id");
  103. String number = jsonChildNode.optString("producto");
  104. String user = jsonChildNode.optString("username");
  105. String cantidad = jsonChildNode.optString("cantidad");
  106. String hora = jsonChildNode.optString("created_at");
  107. String outPut = "NĀ°" + name + " - " + "Usuario: " + user + " - " + "Producto: " + number + " - " + "Cantidad: " + cantidad + " - " + "Fecha: " + hora;
  108. employeeList.add(createEmployee("employees", outPut));
  109. }
  110. } catch (JSONException e) {
  111. Toast.makeText(getApplicationContext(), "Error" + e.toString(),
  112. Toast.LENGTH_SHORT).show();
  113. }
  114.  
  115. SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
  116. android.R.layout.simple_list_item_1,
  117. new String[] { "employees" }, new int[] { android.R.id.text1 });
  118. listView.setAdapter(simpleAdapter);
  119. }
  120.  
  121. private HashMap<String, String> createEmployee(String name, String number) {
  122. HashMap<String, String> employeeNameNo = new HashMap<String, String>();
  123. employeeNameNo.put(name, number);
  124. return employeeNameNo;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement