Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. @Override
  2. protected Void doInBackground(Void... arg0) {
  3. HttpHandler sh = new HttpHandler();
  4. // Making a request to url and getting response
  5. String url = "https://sfasys:sfa321@nfdapp.nabatigroup.com/sap/opu/odata/sap/ZPRJ_SD0003_SRV/SFASTOCKSet?$filter=Werks%20eq%20%27P174%27%20and%20Lgort%20eq%20%27GS00%27$";
  6. // String url = "http://api.androidhive.info/contacts/";
  7. String jsonStr = sh.makeServiceCall(url);
  8.  
  9. Log.e(TAG, "Response from url: " + jsonStr);
  10. if (jsonStr != null) {
  11. try {
  12. JSONObject jsonObj = new JSONObject(jsonStr);
  13.  
  14. // Getting JSON Array node
  15. JSONObject jso = jsonObj.getJSONObject("d");
  16. JSONArray contacts = jso.getJSONArray("results");
  17.  
  18. // looping through All Contacts
  19. for (int i = 0; i < contacts.length(); i++) {
  20. JSONObject c = contacts.getJSONObject(i);
  21. String pcode = c.getString("Matnr");
  22. String stock = c.getString("Labst");
  23.  
  24. // tmp hash map for single contact
  25. HashMap<String, String> contact = new HashMap<>();
  26.  
  27. // adding each child node to HashMap key => value
  28. contact.put("pcode", pcode);
  29. contact.put("stock", stock);
  30.  
  31. // adding contact to contact list
  32. contactList.add(contact);
  33. }
  34. } catch (final JSONException e) {
  35. Log.e(TAG, "Json parsing error: " + e.getMessage());
  36. runOnUiThread(new Runnable() {
  37. @Override
  38. public void run() {
  39. Toast.makeText(getApplicationContext(),
  40. "Json parsing error: " + e.getMessage(),
  41. Toast.LENGTH_LONG).show();
  42. }
  43. });
  44.  
  45. }
  46.  
  47. } else {
  48. Log.e(TAG, "Couldn't get json from server.");
  49. runOnUiThread(new Runnable() {
  50. @Override
  51. public void run() {
  52. Toast.makeText(getApplicationContext(),
  53. "Couldn't get json from server. Check LogCat for possible errors!",
  54. Toast.LENGTH_LONG).show();
  55. }
  56. });
  57. }
  58.  
  59. return null;
  60. }
  61.  
  62. @Override
  63. protected void onPostExecute(Void result) {
  64. super.onPostExecute(result);
  65. ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
  66. R.layout.list_item, new String[]{ "pcode","stock"},
  67. new int[]{R.id.pcode, R.id.stock});
  68. lv.setAdapter(adapter);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement