Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.24 KB | None | 0 0
  1. 04-1205: 40: 32.901: D/AllUsers: (1254): {
  2. "message": "DISPLAYED Success",
  3. "userlist": [
  4. {
  5. "id": "2",
  6. "user": "user2"
  7. },
  8. {
  9. "id": "3",
  10. "user": "michel"
  11. },
  12. {
  13. "id": "4",
  14. "user": "georges"
  15. },
  16. {
  17. "id": "5",
  18. "user": "testtest1"
  19. }
  20. ],
  21. "success": 1
  22. }
  23.  
  24. <?php
  25.  
  26. /*
  27. * Following code will list all the emp
  28. */
  29.  
  30. // array for JSON response
  31. $response = array();
  32.  
  33.  
  34. // include db connect class
  35. require_once __DIR__ . '/db_connect.php';
  36.  
  37. // connecting to db
  38. $db = new DB_CONNECT();
  39.  
  40. // get all emp from emp table
  41. $result = mysql_query("SELECT *FROM users") or die(mysql_error());
  42.  
  43. // check for empty result
  44. if (mysql_num_rows($result) > 0) {
  45.  
  46.  
  47. // looping through all results
  48. // emp node
  49. $response["userlist"] = array();
  50.  
  51. while ($row = mysql_fetch_array($result)) {
  52. $response["success"] = 1;
  53. // temp user array
  54. $userlist = array();
  55. $userlist["id"] = $row["id"];
  56. $userlist["user"] = $row["user"];
  57.  
  58.  
  59.  
  60. // push single Employee into final response array
  61. array_push($response["userlist"], $userlist);
  62. }
  63. // success
  64.  
  65. $response["message"] = "DISPLAYED Success";
  66.  
  67. // echoing JSON response
  68. echo json_encode($response);
  69. } else {
  70. // no emp found
  71. $response["success"] = 0;
  72. $response["message"] = "No User found";
  73.  
  74. // echo no users JSON
  75. echo json_encode($response);
  76. }
  77. ?>
  78.  
  79. package com.devleb.loginDemo;
  80.  
  81. import java.io.BufferedReader;
  82. import java.io.IOException;
  83. import java.io.InputStream;
  84. import java.io.InputStreamReader;
  85. import java.io.UnsupportedEncodingException;
  86. import java.util.List;
  87.  
  88. import org.apache.http.HttpEntity;
  89. import org.apache.http.HttpResponse;
  90. import org.apache.http.NameValuePair;
  91. import org.apache.http.client.ClientProtocolException;
  92. import org.apache.http.client.entity.UrlEncodedFormEntity;
  93. import org.apache.http.client.methods.HttpGet;
  94. import org.apache.http.client.methods.HttpPost;
  95. import org.apache.http.client.utils.URLEncodedUtils;
  96. import org.apache.http.impl.client.DefaultHttpClient;
  97. import org.json.JSONException;
  98. import org.json.JSONObject;
  99.  
  100. import android.util.Log;
  101.  
  102. public class JSONParser {
  103.  
  104. static InputStream is = null;
  105. static JSONObject jObj = null;
  106. static String json = "";
  107.  
  108. // constructor
  109. public JSONParser() {
  110.  
  111. }
  112.  
  113. // function get json from url
  114. // by making HTTP POST or GET mehtod
  115. public JSONObject makeHttpRequest(String url, String method,
  116. List<NameValuePair> params) {
  117.  
  118. // Making HTTP request
  119. try {
  120.  
  121. // check for request method
  122. if(method == "POST"){
  123. // request method is POST
  124. // defaultHttpClient
  125. DefaultHttpClient httpClient = new DefaultHttpClient();
  126. HttpPost httpPost = new HttpPost(url);
  127. httpPost.setEntity(new UrlEncodedFormEntity(params));
  128.  
  129. HttpResponse httpResponse = httpClient.execute(httpPost);
  130. HttpEntity httpEntity = httpResponse.getEntity();
  131. is = httpEntity.getContent();
  132.  
  133. }else if(method == "GET"){
  134. // request method is GET
  135. DefaultHttpClient httpClient = new DefaultHttpClient();
  136. String paramString = URLEncodedUtils.format(params, "utf-8");
  137. url += "?" + paramString;
  138. HttpGet httpGet = new HttpGet(url);
  139.  
  140. HttpResponse httpResponse = httpClient.execute(httpGet);
  141. HttpEntity httpEntity = httpResponse.getEntity();
  142. is = httpEntity.getContent();
  143. }
  144.  
  145.  
  146. } catch (UnsupportedEncodingException e) {
  147. e.printStackTrace();
  148. } catch (ClientProtocolException e) {
  149. e.printStackTrace();
  150. } catch (IOException e) {
  151. e.printStackTrace();
  152. }
  153.  
  154. try {
  155. BufferedReader reader = new BufferedReader(new InputStreamReader(
  156. is, "iso-8859-1"), 8);
  157. StringBuilder sb = new StringBuilder();
  158. String line = null;
  159. while ((line = reader.readLine()) != null) {
  160. sb.append(line + "n");
  161. }
  162. is.close();
  163. json = sb.toString();
  164.  
  165. Log.i("TagCovertS", "["+json+"]");
  166.  
  167.  
  168. } catch (Exception e) {
  169. Log.e("Buffer Error", "Error converting result " + e.toString());
  170. }
  171.  
  172. // try parse the string to a JSON object
  173. try {
  174. jObj = new JSONObject(json);
  175. } catch (JSONException e) {
  176. Log.e("JSON Parser", "Error parsing data " + e.toString());
  177. }
  178.  
  179. // return JSON String
  180. return jObj;
  181.  
  182. }
  183. }
  184.  
  185. package com.devleb.loginDemo;
  186.  
  187. import java.util.ArrayList;
  188. import java.util.HashMap;
  189. import java.util.List;
  190.  
  191. import org.apache.http.NameValuePair;
  192. import org.json.JSONArray;
  193. import org.json.JSONException;
  194. import org.json.JSONObject;
  195.  
  196. import android.os.AsyncTask;
  197. import android.os.Bundle;
  198. import android.app.Activity;
  199. import android.app.ListActivity;
  200. import android.content.Intent;
  201. import android.util.Log;
  202. import android.view.Menu;
  203. import android.view.View;
  204. import android.widget.AdapterView;
  205. import android.widget.AdapterView.OnItemClickListener;
  206. import android.widget.ListAdapter;
  207. import android.widget.ListView;
  208. import android.widget.SimpleAdapter;
  209. import android.widget.TextView;
  210. import android.widget.Toast;
  211.  
  212. public class UserListActivity extends ListActivity {
  213.  
  214. JSONParser jsonParser = new JSONParser();
  215.  
  216. ArrayList<HashMap<String, String>> usersList;
  217.  
  218. private static String url_display_user = "http://10.0.3.2/android_connect/display_user.php";
  219.  
  220. // JSON Node names
  221. private static final String TAG_SUCCESS = "success";
  222. private static final String TAG_MESSAGE = "message";
  223.  
  224. private static final String TAG_ID = "id";
  225.  
  226.  
  227. private static final String TAG_USERS = "userlist";
  228.  
  229. private static final String TAG_USER = "user";
  230.  
  231. //private static final String TAG_NAME = "name";
  232.  
  233. // employees JSONArray
  234. JSONArray users = null;
  235.  
  236. @Override
  237. protected void onCreate(Bundle savedInstanceState) {
  238. super.onCreate(savedInstanceState);
  239. setContentView(R.layout.activity_user_list);
  240.  
  241. usersList = new ArrayList<HashMap<String, String>>();
  242.  
  243. new getUserList().execute();
  244.  
  245. // getListView
  246. ListView lv = getListView();
  247.  
  248. lv.setOnItemClickListener(new OnItemClickListener() {
  249.  
  250. @Override
  251. public void onItemClick(AdapterView<?> arg0, View view, int arg2,
  252. long arg3) {
  253.  
  254. // String id = ((TextView) view.findViewById(R.id.uid)).getText()
  255. // .toString();
  256.  
  257. // Intent in = new Intent(getBaseContext(), StatusList.class);
  258. // in.putExtra(TAG_ID, uid);
  259.  
  260. // startActivity(in);
  261. }
  262. });
  263. }
  264.  
  265. class getUserList extends AsyncTask<String, String, String> {
  266.  
  267. /**
  268. * Before starting background thread Show Progress Dialog
  269. * */
  270. @Override
  271. protected void onPreExecute() {
  272. super.onPreExecute();
  273.  
  274. UserListActivity.this.setProgressBarIndeterminateVisibility(true);
  275. }
  276.  
  277. @Override
  278. protected String doInBackground(String... params) {
  279. // TODO Auto-generated method stub
  280.  
  281. // Building Parameters
  282. List<NameValuePair> parametres = new ArrayList<NameValuePair>();
  283. // getting JSON string from URL
  284. JSONObject json = jsonParser.makeHttpRequest(url_display_user,
  285. "GET", parametres);
  286.  
  287. // Check your log cat for JSON reponse
  288. Log.d("All Users: ", json.toString());
  289.  
  290. try {
  291. // Checking for SUCCESS TAG
  292. int success = json.getInt(TAG_SUCCESS);
  293.  
  294. if (success == 1) {
  295. // products found
  296. // Getting Array of Products
  297. users = json.getJSONArray(TAG_USERS);
  298.  
  299. // looping through All Users
  300. for (int i = 0; i < users.length(); i++) {
  301. JSONObject c = users.getJSONObject(i);
  302.  
  303. // Storing each json item in variable
  304. String id = c.getString(TAG_ID);
  305. String user = c.getString(TAG_USER);
  306.  
  307. // creating new HashMap
  308. HashMap<String, String> map = new HashMap<String, String>();
  309.  
  310. // adding each child node to HashMap key => value
  311. map.put(TAG_ID, id);
  312. map.put(TAG_USER, user);
  313.  
  314. // adding HashList to ArrayList
  315. usersList.add(map);
  316.  
  317. return json.getString(TAG_MESSAGE);
  318. }
  319. } else {
  320.  
  321. return json.getString(TAG_MESSAGE);
  322.  
  323. }
  324. } catch (JSONException e) {
  325. e.printStackTrace();
  326. }
  327.  
  328. return null;
  329. }
  330.  
  331. /**
  332. * After completing background task Dismiss the progress dialog
  333. * **/
  334. protected void onPostExecute(String result) {
  335. // dismiss the dialog after getting all products
  336. if (result != null) {
  337.  
  338. UserListActivity.this
  339. .setProgressBarIndeterminateVisibility(false);
  340. // updating UI from Background Thread
  341. runOnUiThread(new Runnable() {
  342. public void run() {
  343. /**
  344. * Updating parsed JSON data into ListView
  345. * */
  346. ListAdapter adapter = new SimpleAdapter(
  347. UserListActivity.this, usersList,
  348. R.layout.list_item, new String[] { TAG_ID,
  349. TAG_USER }, new int[] { R.id.uid,
  350. R.id.name });
  351. // updating listview
  352. setListAdapter(adapter);
  353. }
  354. });
  355.  
  356. Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG)
  357. .show();
  358.  
  359. }
  360. }
  361.  
  362. }
  363.  
  364. @Override
  365. public boolean onCreateOptionsMenu(Menu menu) {
  366. // Inflate the menu; this adds items to the action bar if it is present.
  367. getMenuInflater().inflate(R.menu.user_list, menu);
  368. return true;
  369. }
  370.  
  371. }
  372.  
  373. <?xml version="1.0" encoding="utf-8"?>
  374. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  375. android:layout_width="match_parent"
  376. android:layout_height="match_parent"
  377. android:orientation="vertical"
  378. android:background="@drawable/background_1">
  379. <ListView
  380. android:id="@android:id/list"
  381. android:layout_width="match_parent"
  382. android:layout_height="match_parent" />
  383. </LinearLayout>
  384.  
  385. <?xml version="1.0" encoding="utf-8"?>
  386. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  387. android:layout_width="fill_parent"
  388. android:layout_height="wrap_content"
  389. android:orientation="vertical" >
  390.  
  391. <TextView
  392. android:id="@+id/uid"
  393. android:layout_width="fill_parent"
  394. android:layout_height="wrap_content"
  395. android:visibility="gone" />
  396.  
  397. <TextView
  398. android:id="@+id/name"
  399. android:layout_width="fill_parent"
  400. android:layout_height="wrap_content"
  401. android:paddingLeft="6dip"
  402. android:paddingTop="6dip"
  403. android:textColor="#FFFFFF"
  404. android:textSize="17dip"
  405. android:textStyle="bold" />
  406.  
  407. </LinearLayout>
  408.  
  409. for (int i = 0; i < users.length(); i++) {
  410. JSONObject c = users.getJSONObject(i);
  411.  
  412. // Storing each json item in variable
  413. String id = c.getString(TAG_ID);
  414. String user = c.getString(TAG_USER);
  415.  
  416. // creating new HashMap
  417. HashMap<String, String> map = new HashMap<String, String>();
  418.  
  419. // adding each child node to HashMap key => value
  420. map.put(TAG_ID, id);
  421. map.put(TAG_USER, user);
  422.  
  423. // adding HashList to ArrayList
  424. usersList.add(map);
  425.  
  426. return json.getString(TAG_MESSAGE);
  427. }
  428.  
  429. for (int i = 0; i < users.length(); i++) {
  430. JSONObject c = users.getJSONObject(i);
  431.  
  432. // Storing each json item in variable
  433. String id = c.getString(TAG_ID);
  434. String user = c.getString(TAG_USER);
  435.  
  436. // creating new HashMap
  437. HashMap<String, String> map = new HashMap<String, String>();
  438.  
  439. // adding each child node to HashMap key => value
  440. map.put(TAG_ID, id);
  441. map.put(TAG_USER, user);
  442.  
  443. // adding HashList to ArrayList
  444. usersList.add(map);
  445.  
  446. }
  447. return json.getString(TAG_MESSAGE); <--- move this line after for statement
  448.  
  449. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item,
  450. usersList);
  451. mListView.setCacheColorHint(Color.TRANSPARENT);
  452. mListView.setAdapter(adapter);
  453. adapter.notifyDataSetChanged();
  454. adapter.setNotifyOnChange(false);
  455. mListView.invalidateViews();
  456.  
  457. i want this type response please help me
  458. {
  459. "message": "DISPLAYED Success",
  460. "userlist": [
  461. {
  462. "id": "2",
  463. "user": "user2"
  464. },
  465. {
  466. "id": "3",
  467. "user": "michel"
  468. },
  469. {
  470. "id": "4",
  471. "user": "georges"
  472. },
  473. {
  474. "id": "5",
  475. "user": "testtest1"
  476. }
  477. ],
  478. "success": 1
  479. }
  480.  
  481. i am using this code
  482.  
  483.  
  484. <?php
  485.  
  486. $servername = "localhost";
  487. $username = "root";
  488. $password = "";
  489. $dbname = "text_api";
  490.  
  491. /*
  492. * Following code will list all the emp
  493. */
  494.  
  495. // array for JSON response
  496. $response = array();
  497.  
  498.  
  499. // Create connection
  500. $conn = new mysqli($servername, $username, $password, $dbname);
  501. // Check connection
  502. if ($conn->connect_error) {
  503. die("Connection failed: " . $conn->connect_error);
  504. }
  505. $SQL = "SELECT `id`, `name`, `email`, `password`, `created_at`, `updated_at` FROM `users` ";
  506. // $result = mysqli_query($conn,$SQL);
  507. // get all emp from emp table
  508. $result = mysql_query("SELECT `id`, `name`, `email`, `password`, `created_at`, `updated_at` FROM `users` ") or die(mysql_error());
  509.  
  510. //// get all emp from emp table
  511. // check for empty result
  512.  
  513. if (mysql_num_rows($result) > 0) {
  514.  
  515.  
  516. // looping through all results
  517. // emp node
  518. $response["userlist"] = array();
  519.  
  520. while ($row = mysql_fetch_array($result)) {
  521.  
  522.  
  523. $response["success"] = 1;
  524. // temp user array
  525. $userlist = array();
  526. $userlist["id"] = $row["id"];
  527. $userlist["name"] = $row["name"];
  528. $userlist["email"] = $row["email"];
  529. $userlist["password"] = $row["password"];
  530. $userlist["created_at"] = $row["created_at"];
  531. $userlist["updated_at"] = $row["updated_at"];
  532.  
  533.  
  534.  
  535. // push single Employee into final response array
  536. array_push($response["userlist"], $userlist);
  537. }
  538. // success
  539.  
  540. $response["message"] = "DISPLAYED Success";
  541.  
  542. // echoing JSON response
  543. echo json_encode($response);
  544. } else {
  545. // no emp found
  546. $response["success"] = 0;
  547. $response["message"] = "No User found";
  548.  
  549. // echo no users JSON
  550. echo json_encode($response);
  551. }
  552. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement