Guest User

Untitled

a guest
May 4th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. package com.example.loginmysql;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.util.ArrayList;
  11. import org.apache.http.HttpEntity;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.NameValuePair;
  14. import org.apache.http.client.HttpClient;
  15. import org.apache.http.client.entity.UrlEncodedFormEntity;
  16. import org.apache.http.client.methods.HttpPost;
  17. import org.apache.http.impl.client.DefaultHttpClient;
  18. import org.apache.http.message.BasicNameValuePair;
  19. import org.json.JSONArray;
  20. import org.json.JSONObject;
  21.  
  22. import android.os.AsyncTask;
  23. import android.os.Bundle;
  24. import android.util.Log;
  25. import android.view.Menu;
  26. import android.view.View;
  27. import android.widget.Button;
  28. import android.widget.EditText;
  29. import android.widget.Toast;
  30.  
  31. public class EditUser extends Activity {
  32. String password;
  33. String username;
  34. InputStream is=null;
  35. String result=null;
  36. String line=null;
  37. int code;
  38. int UserID;
  39.  
  40. Activity context;
  41. HttpPost httppost;
  42. StringBuffer buffer;
  43. HttpResponse response;
  44. HttpClient httpclient;
  45. ProgressDialog pd;
  46.  
  47. @Override
  48. public void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.editnewuser);
  51. context=this;
  52.  
  53. final EditText e_username=(EditText) findViewById(R.id.edtusername);
  54. final EditText e_password=(EditText) findViewById(R.id.edtnewpass);
  55. Button insert=(Button) findViewById(R.id.button1);
  56.  
  57. Bundle extras = getIntent().getExtras();
  58. if(extras !=null)
  59. {
  60. e_username.setText(extras.getString("username"));
  61. e_password.setText(extras.getString("password"));
  62. UserID = extras.getInt("ID");
  63. }
  64.  
  65. insert.setOnClickListener(new View.OnClickListener() {
  66.  
  67. @Override
  68. public void onClick(View v) {
  69. // TODO Auto-generated method stub
  70.  
  71. username = e_username.getText().toString().trim();
  72. password = e_password.getText().toString().trim();
  73. Toast.makeText(getApplicationContext(), "username : "+username + "||" + "password : " + password,
  74. Toast.LENGTH_LONG).show();
  75. BackTask bt=new BackTask();
  76. bt.execute();
  77. }
  78. });
  79.  
  80.  
  81. }
  82.  
  83. public void insert()
  84. {
  85. ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  86. nameValuePairs.clear();
  87. nameValuePairs.add(new BasicNameValuePair("username",username));
  88. nameValuePairs.add(new BasicNameValuePair("password",password));
  89. nameValuePairs.add(new BasicNameValuePair("ID",String.valueOf(UserID)));
  90.  
  91. try
  92. {
  93. HttpClient httpclient = new DefaultHttpClient();
  94. HttpPost httppost = new HttpPost("http://mobilekampus.com/ras/update_masteruser.php");
  95. httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  96. httpclient.execute(httppost);
  97. /* HttpResponse response = httpclient.execute(httppost);
  98. HttpEntity entity = response.getEntity();
  99. is = entity.getContent();
  100. Log.e("pass 1", "connection success ");*/
  101. }
  102. catch(Exception e)
  103. {
  104. Log.e("Fail 1", e.toString());
  105. Toast.makeText(getApplicationContext(), "Invalid IP Address",
  106. Toast.LENGTH_LONG).show();
  107. }
  108.  
  109. try
  110. {
  111. BufferedReader reader = new BufferedReader
  112. (new InputStreamReader(is,"iso-8859-1"),8);
  113. StringBuilder sb = new StringBuilder();
  114. while ((line = reader.readLine()) != null)
  115. {
  116. sb.append(line + "\n");
  117. }
  118. is.close();
  119. result = sb.toString();
  120. Log.e("pass 2", "connection success ");
  121. }
  122. catch(Exception e)
  123. {
  124. Log.e("Fail 2", e.toString());
  125. }
  126.  
  127. /*try
  128. {
  129. JSONObject json_data = new JSONObject(result);
  130. code=(json_data.getInt("code"));
  131.  
  132. if(code==1)
  133. {
  134. Toast.makeText(getBaseContext(), "Inserted Successfully",
  135. Toast.LENGTH_SHORT).show();
  136. }
  137. else
  138. {
  139. Toast.makeText(getBaseContext(), "Sorry, Try Again",
  140. Toast.LENGTH_LONG).show();
  141. }
  142. }
  143. catch(Exception e)
  144. {
  145. Log.e("Fail 3", e.toString());
  146. }*/
  147. }
  148.  
  149. //background process to make a request to server and list product information
  150. private class BackTask extends AsyncTask<Void,Void,Void>{
  151.  
  152. protected void onPreExecute(){
  153. super.onPreExecute();
  154. pd = new ProgressDialog(context);
  155. pd.setTitle("Updating ID : " + String.valueOf(UserID));
  156. pd.setMessage("Please wait.");
  157. pd.setCancelable(true);
  158. pd.setIndeterminate(true);
  159. pd.show();
  160. }
  161.  
  162. protected Void doInBackground(Void...params){
  163.  
  164. InputStream is=null;
  165.  
  166. String result="";
  167. ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  168. nameValuePairs.clear();
  169. nameValuePairs.add(new BasicNameValuePair("username",username));
  170. nameValuePairs.add(new BasicNameValuePair("password",password));
  171. nameValuePairs.add(new BasicNameValuePair("ID",String.valueOf(UserID)));
  172.  
  173. try
  174. {
  175. httpclient = new DefaultHttpClient();
  176. httppost = new HttpPost("http://mobilekampus.com/ras/update_masteruser.php");
  177. httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  178. //httpclient.execute(httppost);
  179. response = httpclient.execute(httppost);
  180. HttpEntity entity = response.getEntity();
  181. is = entity.getContent();
  182. Log.e("pass 1", "connection success ");
  183. }
  184. catch(Exception e)
  185. {
  186. if(pd!=null)
  187. pd.dismiss(); //close the dialog if error occurs
  188. Log.e("ERROR", e.getMessage());
  189. }
  190.  
  191. //convert response to string
  192. try{
  193. BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
  194. StringBuilder sb = new StringBuilder();
  195. String line = null;
  196. while ((line = reader.readLine()) != null) {
  197. sb.append(line+"\n");
  198.  
  199. }
  200.  
  201. is.close();
  202. result=sb.toString();
  203.  
  204. }catch(Exception e){
  205. Log.e("ERROR", "Error converting result "+e.toString());
  206. }
  207.  
  208. //parse json data
  209. try{
  210. // Remove unexpected characters that might be added to beginning of the string
  211. result =result.substring(result.indexOf("["));
  212. JSONArray jArray =new JSONArray(result);
  213.  
  214. for(int i=0;i<jArray.length();i++){
  215. JSONObject json_data =jArray.getJSONObject(i);
  216. code=(json_data.getInt("code"));
  217. }
  218.  
  219. if(code==1)
  220. {
  221. Toast.makeText(getBaseContext(), "Updated Successfully",
  222. Toast.LENGTH_SHORT).show();
  223. }
  224. else
  225. {
  226. Toast.makeText(getBaseContext(), "Sorry, Try Again",
  227. Toast.LENGTH_LONG).show();
  228. }
  229. }
  230. catch(Exception e){
  231. Log.e("ERROR", "Error pasting data "+e.toString());
  232. }
  233. return null;
  234. }
  235.  
  236. protected void onPostExecute(Void result){
  237. if(pd!=null) pd.dismiss(); //close dialog
  238. context.startActivity(new Intent(context, ViewUser.class));
  239. }
  240.  
  241. }
  242.  
  243. @Override
  244. public boolean onCreateOptionsMenu(Menu menu) {
  245. getMenuInflater().inflate(R.menu.main, menu);
  246. return true;
  247. }
  248.  
  249. @Override
  250. public void onBackPressed() {
  251. // Do Here what ever you want do on back press;
  252. Toast.makeText(getApplicationContext(),"You are not allowed to go back!",Toast.LENGTH_LONG).show();
  253. }
  254.  
  255. }
Add Comment
Please, Sign In to add comment