PostComment.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.ivalentin.gm;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.os.AsyncTask;
  5. import android.text.format.DateFormat;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.ImageView;
  12. import android.widget.LinearLayout;
  13. import android.widget.ProgressBar;
  14. import android.widget.TextView;
  15. import java.io.DataOutputStream;
  16. import java.net.HttpURLConnection;
  17. import java.net.URL;
  18. import java.net.URLEncoder;
  19. import java.nio.charset.StandardCharsets;
  20. import java.util.Date;
  21. class PostComment extends AsyncTask<String, String, Integer> {
  22. //Elements passed from fragments
  23. String type, user, text, language;
  24. LinearLayout list, form;
  25. TextView counter;
  26. int id, currentCounter;
  27. Context context;
  28. //Elements calculated here
  29. Button btSend;
  30. ProgressBar pb;
  31. int code = 404;
  32. public PostComment(String type, String user, String text, String language, int id, LinearLayout form, LinearLayout list, int currentCounter, TextView counter, Context context) {
  33. super();
  34. this.type = type;
  35. this.user = user;
  36. this.text = text;
  37. this.form = form;
  38. this.list = list;
  39. this.language = language;
  40. this.counter = counter;
  41. this.id = id;
  42. this.currentCounter = currentCounter;
  43. this.context = context;
  44. }
  45. /**
  46. * Before starting background thread
  47. * */
  48. @Override
  49. protected void onPreExecute() {
  50. Log.e("PRE", "START");
  51. super.onPreExecute();
  52. btSend = (Button) form.findViewById(R.id.bt_post_comment_send);
  53. pb = (ProgressBar) form.findViewById(R.id.pb_comment);
  54. btSend.setVisibility(View.GONE);
  55. pb.setVisibility(View.VISIBLE);
  56. Log.e("PRE", "END");
  57. }
  58. /**
  59. * Downloading file in background thread
  60. * */
  61. @Override
  62. protected Integer doInBackground(String... f_url) {
  63. Log.e("DIB", "START");
  64. int count;
  65. URL url;
  66. String urlString, urlParams;
  67. try {
  68. //url = new URL(GM.SERVER + "/" + type + "/comment.php?user=" + user + "&text=" + text);
  69. urlString = GM.SERVER + "/" + type + "/comment.php?user=" + URLEncoder.encode(user) + "&text=" + URLEncoder.encode(text);
  70. urlParams = "user=" + URLEncoder.encode(user) + "&text=" + URLEncoder.encode(text);
  71. Log.e("TYPE", type);
  72. switch (type){
  73. case "blog":
  74. urlParams = urlParams + "&post=" + id;
  75. break;
  76. case "galeria":
  77. urlParams = urlParams + "&photo=" + id;
  78. break;
  79. case "actividades":
  80. urlParams = urlParams + "&activity=" + id;
  81. break;
  82. default:
  83. //TODO, error, stop
  84. }
  85. switch (language){
  86. case "es":
  87. urlParams = urlParams + "&lang=es";
  88. break;
  89. case "eu":
  90. urlParams = urlParams + "&lang=eu";
  91. break;
  92. default:
  93. this.language = "en";
  94. urlParams = urlParams + "&lang=en";
  95. }
  96. byte[] postData = urlParams.getBytes("UTF-8");
  97. int postDataLength = postData.length;
  98. String request = GM.SERVER + "/" + type + "/comment.php";
  99. url = new URL( request );
  100. HttpURLConnection conn= (HttpURLConnection) url.openConnection();
  101. conn.setDoOutput( true );
  102. conn.setInstanceFollowRedirects( false );
  103. conn.setRequestMethod( "POST" );
  104. conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
  105. conn.setRequestProperty( "charset", "utf-8");
  106. conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
  107. conn.setUseCaches( false );
  108. DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
  109. wr.write(postData);
  110. conn.connect();
  111. Log.e("URL", urlString);
  112. //url = new URL(urlString);
  113. //HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  114. //connection.setRequestMethod("GET");
  115. //connection.connect();
  116. code = conn.getResponseCode();
  117. Log.d("Comment status", "" + code);
  118. if (code == 200){
  119. //TODO: Insert into local db
  120. SQLiteDatabase db = context.openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
  121. String table = "";
  122. switch (type){
  123. case "blog":
  124. table = "post_comment";
  125. break;
  126. case "galeria":
  127. table = "photo_comment";
  128. break;
  129. case "actividades":
  130. table = "activity_comment";
  131. break;
  132. }
  133. db.rawQuery("INSERT INTO " + table + " (post, text, username, lang) VALUES (" + id + ", '" + text + "', '" + user + "', '" + language + "');", null);
  134. db.close();
  135. }
  136. } catch (Exception e) {
  137. Log.e("Error posting: ", e.getMessage());
  138. }
  139. Log.e("DIB", "END");
  140. return code;
  141. }
  142. /**
  143. * After completing background task
  144. * **/
  145. @Override
  146. protected void onPostExecute(Integer a) {
  147. Log.e("POS", "START");
  148. //Log.d("Comment posted", type);
  149. if (code == 200) {
  150. //Clear form
  151. EditText formText = (EditText) form.findViewById(R.id.et_post_comment_text);
  152. EditText formUser = (EditText) form.findViewById(R.id.et_post_comment_name);
  153. formUser.setText("");
  154. formText.setText("");
  155. //TODO: Update counter
  156. //TODO: Insert comment in list
  157. LayoutInflater factory = LayoutInflater.from(context);
  158. LinearLayout entry = (LinearLayout) factory.inflate(R.layout.row_comment, null);
  159. //Set user
  160. TextView tvUser = (TextView) entry.findViewById(R.id.tv_row_comment_user);
  161. tvUser.setText(user);
  162. Date d = new Date();
  163. String date = DateFormat.format("yyyy-MM-dd hh:mm:ss", d.getTime()).toString();
  164. TextView tvCDate = (TextView) entry.findViewById(R.id.tv_row_comment_date);
  165. tvCDate.setText(GM.formatDate(date, language, true));
  166. TextView tvText = (TextView) entry.findViewById(R.id.tv_row_comment_text);
  167. tvText.setText(text);
  168. //Add to the list
  169. list.addView(entry);
  170. }
  171. //Show button again
  172. btSend.setVisibility(View.VISIBLE);
  173. pb.setVisibility(View.GONE);
  174. //ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
  175. Log.e("POS", "END");
  176. }
  177. }