Advertisement
Guest User

Untitled

a guest
May 29th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. package com.kikigames.Activitis;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.EditText;
  12.  
  13. import com.google.gson.Gson;
  14. import com.google.gson.GsonBuilder;
  15. import com.kikigames.com.shouter.entities.JMessage;
  16. import com.kikigames.com.shouter.entities.JUser;
  17. import com.kikigames.menudemo2.ListGroups;
  18. import com.kikigames.menudemo2.R;
  19.  
  20. import org.apache.http.HttpResponse;
  21. import org.apache.http.client.HttpClient;
  22. import org.apache.http.client.methods.HttpPost;
  23. import org.apache.http.impl.client.DefaultHttpClient;
  24. import org.apache.http.util.EntityUtils;
  25.  
  26. import java.net.URLEncoder;
  27.  
  28.  
  29. public class NewMessageActivity extends Activity {
  30.  
  31.  
  32. private static final String TAG = ListGroups.class.getSimpleName();
  33. EditText etMsg;
  34. String msg;
  35. JUser user;
  36.  
  37. private static Gson gson = new GsonBuilder().setDateFormat(
  38. "yyyy-MM-dd HH:mm:ss").create();
  39.  
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_new_message);
  44. String c = getIntent().getStringExtra("json");
  45.  
  46. etMsg = (EditText) findViewById(R.id.editText);
  47.  
  48.  
  49. }
  50.  
  51.  
  52. public void onClickSendMessage(View v){
  53. msg = etMsg.getText().toString();
  54.  
  55. String userAux = "{\"userId\":2,\"userName\":\"sau\",\"userImage\":\"samu\",\"userTsCreation\":\"2015-01-01 00:00:00\",\"userEmail\":\"samu\",\"userBanned\":false,\"userHidden\":false,\"userStrikes\":0,\"userPuntuation\":0.0}";
  56. user = gson.fromJson(userAux, JUser.class);
  57.  
  58. JMessage jm = new JMessage(msg,user);
  59. Log.d(TAG, "json = " + gson.toJson(jm).toString());
  60. NewMessage nm = new NewMessage(gson.toJson(jm).toString());
  61. nm.execute();
  62. }
  63.  
  64. @Override
  65. public void onBackPressed()
  66. {
  67. //do whatever you want the 'Back' button to do
  68. //as an example the 'Back' button is set to start a new Activity named 'NewActivity'
  69. this.startActivity(new Intent(NewMessageActivity.this, MainActivity.class));
  70. finish();
  71. return;
  72. }
  73.  
  74. @Override
  75. public boolean onCreateOptionsMenu(Menu menu) {
  76. // Inflate the menu; this adds items to the action bar if it is present.
  77. getMenuInflater().inflate(R.menu.menu_new_message, menu);
  78. return true;
  79. }
  80.  
  81. @Override
  82. public boolean onOptionsItemSelected(MenuItem item) {
  83. // Handle action bar item clicks here. The action bar will
  84. // automatically handle clicks on the Home/Up button, so long
  85. // as you specify a parent activity in AndroidManifest.xml.
  86. int id = item.getItemId();
  87.  
  88. //noinspection SimplifiableIfStatement
  89. if (id == R.id.action_settings) {
  90. return true;
  91. }
  92.  
  93. return super.onOptionsItemSelected(item);
  94. }
  95. }
  96.  
  97.  
  98. class NewMessage extends AsyncTask<String,Integer,Boolean> {
  99.  
  100. private static final String TAG = ListGroups.class.getSimpleName();
  101.  
  102. private static Gson gson = new GsonBuilder().setDateFormat(
  103. "yyyy-MM-dd HH:mm:ss").create();
  104. String respStr;
  105. String jsonMessage="";
  106.  
  107. public NewMessage(String jsonMessage) {
  108. this.jsonMessage = jsonMessage;
  109. }
  110.  
  111. @Override
  112. protected Boolean doInBackground(String... params) {
  113.  
  114. boolean resul = true;
  115. HttpClient httpClient = new DefaultHttpClient();
  116. HttpPost post = new HttpPost("http://immense-badlands-1533.herokuapp.com/addedit/createmessage/"+ URLEncoder.encode(jsonMessage));
  117. post.setHeader("content-type", "application/json");
  118.  
  119. try {
  120. HttpResponse resp = httpClient.execute(post);
  121. respStr = EntityUtils.toString(resp.getEntity());
  122.  
  123. } catch (Exception ex) {
  124. Log.e("ServicioRest", "Error!", ex);
  125. resul = false;
  126. }
  127.  
  128. return resul;
  129. }
  130.  
  131. protected void onPostExecute(Boolean result) {
  132. if(result){
  133. Log.d(TAG, "json = " + respStr);
  134.  
  135. try {
  136. //JGroup jg = gson.fromJson(respStr, JGroup.class);
  137. //working on it
  138. }catch(Exception e){
  139. e.printStackTrace();
  140.  
  141. }
  142. }
  143. }
  144.  
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement