Advertisement
Guest User

Untitled

a guest
Mar 6th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 KB | None | 0 0
  1. package com.prgguru.jersey;
  2.  
  3. import javax.ws.rs.Consumes;
  4. import javax.ws.rs.GET;
  5. import javax.ws.rs.POST;
  6. import javax.ws.rs.Path;
  7. import javax.ws.rs.PathParam;
  8. import javax.ws.rs.Produces;
  9. import javax.ws.rs.QueryParam;
  10. import javax.ws.rs.core.MediaType;
  11.  
  12. //Path: http://localhost/<appln-folder-name>/register
  13. @Path("/reg")
  14. public class reg {
  15. // HTTP POST Method
  16. @POST
  17. // Path: http://localhost/<appln-folder-name>/register/doregister
  18. @Path("/doreg")
  19. // Produces JSON as response
  20.  
  21. @Produces(MediaType.APPLICATION_JSON)
  22. @Consumes(MediaType.APPLICATION_JSON)
  23. // Query parameters are parameters: http://localhost/<appln-folder-name>/register/doregister?name=pqrs&username=abc&password=xyz
  24. public String doReg( @QueryParam("username") String uname, @QueryParam("password") String pwd, @QueryParam("photo") String pht){
  25. //public String doReg( @PathParam("username") String uname, @PathParam("password") String pwd, @PathParam("photo") String pht){
  26. String response = "";
  27. System.out.println(uname+" "+pwd+" "+pht+" ");
  28.  
  29. int retCode = registerUser(uname, pwd, pht);
  30.  
  31. if(retCode == 0){
  32. response = Utitlity.constructJSON("register",true);
  33. }else if(retCode == 1){
  34. response = Utitlity.constructJSON("register",false, "You are already registered");
  35. }else if(retCode == 2){
  36. response = Utitlity.constructJSON("register",false, "Special Characters are not allowed in Username and Password");
  37. }else if(retCode == 3){
  38. response = Utitlity.constructJSON("register",false, "Error occured");
  39. }
  40. return response;
  41.  
  42. }
  43.  
  44. private int registerUser(String uname, String pwd,String photo) {
  45. System.out.println("Inside checkCredentials");
  46. System.out.println(uname+" "+pwd+" "+photo);
  47. int result = 3;
  48. if(Utitlity.isNotNull(uname) && Utitlity.isNotNull(pwd)){
  49. System.out.println("passou no controlo do nulo");
  50. try{
  51. if(DBConnection.checkLogin(uname, pwd)){
  52. System.out.println("");
  53. result = 1;
  54. } else {
  55. DBConnection.insertUser(uname, pwd, photo);
  56. result = 0;
  57. }
  58. } catch (Exception e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. result = 3;
  62. }
  63. }
  64.  
  65. return result;
  66. }
  67. }
  68.  
  69. package com.example.pedrooliveira.jsontest;
  70.  
  71. import android.app.Activity;
  72. import android.app.ProgressDialog;
  73. import android.content.Intent;
  74. import android.database.Cursor;
  75. import android.net.Uri;
  76. import android.os.Bundle;
  77. import android.provider.MediaStore;
  78. import android.util.Base64;
  79. import android.util.Log;
  80. import android.view.View;
  81. import android.widget.EditText;
  82. import android.widget.Toast;
  83.  
  84. import org.json.JSONException;
  85. import org.json.JSONObject;
  86. import com.loopj.android.http.AsyncHttpClient;
  87. import com.loopj.android.http.AsyncHttpResponseHandler;
  88. import com.loopj.android.http.RequestParams;
  89.  
  90. import java.io.File;
  91. import java.io.FileInputStream;
  92. import java.io.IOException;
  93. import java.io.InputStream;
  94.  
  95.  
  96.  
  97. public class MainActivity extends Activity {
  98.  
  99.  
  100. EditText password;
  101. EditText username;
  102. ProgressDialog prgDialog;
  103. private static int RESULT_LOAD_IMG = 1;
  104. String photo = "default";
  105.  
  106.  
  107.  
  108. public void onCreate(Bundle savedInstanceState) {
  109. super.onCreate(savedInstanceState);
  110. setContentView(R.layout.activity_main);
  111. prgDialog = new ProgressDialog(this);
  112. // Set Progress Dialog Text
  113. prgDialog.setMessage("Please wait...");
  114. // Set Cancelable as False
  115. prgDialog.setCancelable(false);
  116. password = (EditText) findViewById(R.id.password);
  117. username = (EditText) findViewById(R.id.username);
  118.  
  119.  
  120. }
  121.  
  122.  
  123. public void uploadPhoto(View view){
  124. Intent galleryIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  125. // Start the Intent
  126. startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
  127.  
  128. }
  129.  
  130. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  131. super.onActivityResult(requestCode, resultCode, data);
  132. try {
  133. // When an Image is picked
  134. if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
  135. // Get the Image from data
  136.  
  137.  
  138. Uri selectedImage = data.getData();
  139.  
  140.  
  141. String[] filePathColumn = { MediaStore.Images.Media.DATA };
  142. Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
  143. cursor.moveToFirst();
  144. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  145. String picturePath = cursor.getString(columnIndex);
  146. cursor.close();
  147.  
  148. File f = new File(picturePath);
  149.  
  150. String imageName = f.getName();
  151.  
  152. byte[] userphoto = readImageOnlyBytes(f);
  153.  
  154. photo = Base64.encodeToString(userphoto ,Base64.DEFAULT);
  155.  
  156. } else {
  157. Toast.makeText(this, "You haven't picked Image",Toast.LENGTH_LONG).show();
  158. }
  159. } catch (Exception e) {
  160. Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
  161. }
  162.  
  163. }
  164.  
  165.  
  166. public static byte[] readImageOnlyBytes(File file) throws IOException {
  167.  
  168. //Logger.getLogger(Main.class.getName()).log(Level.INFO, "[Open File] " + file.getAbsolutePath());
  169. InputStream is = new FileInputStream(file);
  170. // Get the size of the file
  171. long length = file.length();
  172. // You cannot create an array using a long type.
  173. // It needs to be an int type.
  174. // Before converting to an int type, check
  175. // to ensure that file is not larger than Integer.MAX_VALUE.
  176. if (length > Integer.MAX_VALUE)
  177. {
  178. // File is too large
  179. }
  180. // Create the byte array to hold the data
  181. byte[] bytes = new byte[(int) length];
  182. // Read in the bytes
  183. int offset = 0;
  184. int numRead = 0;
  185.  
  186. while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)
  187. {
  188. offset += numRead;
  189. }
  190. // Ensure all the bytes have been read in
  191. if (offset < bytes.length)
  192. {
  193. throw new IOException("Could not completely read file " + file.getName());
  194. }
  195. // Close the input stream and return bytes
  196. is.close();
  197. return bytes;
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. public void loginUser(View view){
  213. String pwd = password.getText().toString();
  214. String usr = username.getText().toString();
  215. String pht = this.photo;
  216.  
  217. RequestParams params = new RequestParams();
  218.  
  219. params.put("password", pwd);
  220. params.put("username", usr);
  221. params.put("photo", pht);
  222.  
  223.  
  224. // Put Http parameter password with value of Password Edit Value control
  225.  
  226.  
  227. invokeWS(params);
  228.  
  229. }
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236. public void invokeWS(RequestParams params){
  237. // Show Progress Dialog
  238. prgDialog.show();
  239. // Make RESTful webservice call using AsyncHttpClient object
  240. AsyncHttpClient client = new AsyncHttpClient();
  241.  
  242. Log.d("params: ",params.toString());
  243. //Log.d("Parameters",params.toString());
  244. client.post(getApplicationContext(),"http://192.168.2.84:8080/useraccount/reg/doreg", (RequestParams) params, new AsyncHttpResponseHandler() {
  245.  
  246. @Override
  247. public void onSuccess(String response) {
  248. // Hide Progress Dialog
  249. prgDialog.dismiss();
  250. try {
  251. // JSON Object
  252. JSONObject obj = new JSONObject(response);
  253. // When the JSON response has status boolean value assigned with true
  254. if (obj.getBoolean("status")) {
  255. Toast.makeText(getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
  256. // Navigate to Home screen
  257. Log.d("Suuu", "a");
  258. }
  259. // Else display error message
  260. else {
  261.  
  262. Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
  263. }
  264. } catch (JSONException e) {
  265. // TODO Auto-generated catch block
  266. Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
  267. e.printStackTrace();
  268.  
  269. }
  270. }
  271.  
  272. // When the response returned by REST has Http response code other than '200'
  273.  
  274. //@Override
  275. public void onFailure(int statusCode, Throwable error, String content) {
  276. // Hide Progress Dialog
  277. prgDialog.hide();
  278. // When Http response code is '404'
  279. if (statusCode == 404) {
  280. Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
  281. }
  282. // When Http response code is '500'
  283. else if (statusCode == 500) {
  284. Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
  285. }
  286. // When Http response code other than 404, 500
  287. else {
  288. Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
  289. }
  290. }
  291. });
  292. }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement