Advertisement
Guest User

Untitled

a guest
Mar 13th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_comment);
  5.  
  6. ActionBar actionbar = getSupportActionBar();
  7. actionbar.setDisplayHomeAsUpEnabled(true);
  8. abm = new ActionBarMenu(CommentActivityWp.this);
  9.  
  10. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  11. StrictMode.setThreadPolicy(policy);
  12.  
  13. SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
  14. userid = sp.getString("user_id", "none");
  15.  
  16. Intent i = getIntent();
  17. // Receiving the Data
  18. ida = i.getStringExtra("id");
  19. url = "http://www.cepfonline.org/cepfblog/?json=1=" + ida;
  20.  
  21. if (com.cepfmobileapp.org.service.InternetStatus.getInstance(this)
  22. .isOnline(this)) {
  23.  
  24. // Toast t = Toast.makeText(this,"You are online!!!!",8000).show();
  25. // Toast.makeText(getBaseContext(),"You are online",Toast.LENGTH_SHORT).show();
  26. // Calling async task to get json
  27. new GetQuery().execute();
  28.  
  29. } else {
  30. // Toast.makeText(getBaseContext(),"No Internet Connection",Toast.LENGTH_LONG).show();
  31.  
  32. AlertDialog NetAlert = new AlertDialog.Builder(CommentActivityWp.this)
  33. .create();
  34. NetAlert.setMessage("No Internet Connection Found! Please check your connection and try again!");
  35. NetAlert.setButton("OK", new DialogInterface.OnClickListener() {
  36. public void onClick(DialogInterface dialog, int which) {
  37.  
  38. // here you can add functions
  39. // finish();
  40. }
  41. });
  42. NetAlert.show();
  43. }
  44.  
  45. queryList = new ArrayList<HashMap<String, String>>();
  46.  
  47. ListView lv = getListView();
  48.  
  49. // Listview on item click listener
  50. lv.setOnItemClickListener(new OnItemClickListener() {
  51.  
  52. @Override
  53. public void onItemClick(AdapterView<?> parent, View view,
  54. int position, long id) {
  55.  
  56. }
  57. });
  58. }
  59.  
  60. /**
  61. * Async task class to get json by making HTTP call
  62. * */
  63. private class GetQuery extends AsyncTask<Void, Void, Void> {
  64.  
  65. @Override
  66. protected void onPreExecute() {
  67. super.onPreExecute();
  68. // Showing progress dialog
  69. pDialog = new ProgressDialog(CommentActivityWp.this);
  70. pDialog.setMessage("Loading...comments");
  71. pDialog.setCancelable(false);
  72. pDialog.show();
  73.  
  74. }
  75.  
  76. @Override
  77. protected Void doInBackground(Void... arg0) {
  78. // Creating service handler class instance
  79. ServiceHandler sh = new ServiceHandler();
  80.  
  81. // Making a request to url and getting response
  82. String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
  83.  
  84. Log.d("Response: ", "> " + jsonStr);
  85.  
  86. if (jsonStr != null) {
  87. try {
  88. JSONObject jsonObj = new JSONObject(jsonStr);
  89.  
  90. // Getting JSON Array node
  91. comments = jsonObj.getJSONArray(TAG_COMMENTS);
  92.  
  93. // looping through All Contacts
  94. for (int i = 0; i < comments.length(); i++) {
  95. JSONObject c = comments.getJSONObject(i);
  96.  
  97.  
  98. // Phone node is JSON Object
  99. //JSONObject comments = c.getJSONObject(TAG_COMMENTS );
  100. String id = c.getString(TAG_ID);
  101. String name = c.getString(TAG_NAME);
  102. String content = c.getString(TAG_CONTENT);
  103.  
  104. // tmp hashmap for single contact
  105. HashMap<String, String> contact = new HashMap<String, String>();
  106.  
  107. // adding each child node to HashMap key => value
  108. contact.put(TAG_ID, id);
  109. contact.put(TAG_NAME, name);
  110. contact.put(TAG_CONTENT, content);
  111.  
  112.  
  113. // adding contact to contact list
  114. queryList.add(contact);
  115. }
  116. } catch (JSONException e) {
  117. e.printStackTrace();
  118. }
  119. } else {
  120. Log.e("ServiceHandler", "Couldn't get any data from the url");
  121. }
  122.  
  123. return null;
  124. }
  125.  
  126. @Override
  127. protected void onPostExecute(Void result) {
  128. super.onPostExecute(result);
  129. // Dismiss the progress dialog
  130. if (pDialog.isShowing())
  131. pDialog.dismiss();
  132. /**
  133. * Updating parsed JSON data into ListView
  134. * */
  135. ListAdapter adapter = new SimpleAdapter(CommentActivityWp.this,
  136. queryList, R.layout.list_itemwpcmts, new String[] { TAG_NAME, TAG_CONTENT }, new int[] {R.id.name, R.id.content });
  137.  
  138. setListAdapter(adapter);
  139. }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement