Guest User

Untitled

a guest
Nov 18th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.98 KB | None | 0 0
  1. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
  2. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
  3. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
  4. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
  5. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at com.techrefic.app1611.MainActivity$2.onResponse(MainActivity.java:178)
  6. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at com.techrefic.app1611.MainActivity$2.onResponse(MainActivity.java:172)
  7. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
  8. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
  9. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
  10. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
  11. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
  12. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at android.os.Looper.loop(Looper.java:148)
  13. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
  14. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
  15. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  16. 11-18 13:27:23.766 30362-30362/com.techrefic.app1611 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  17. 11-18 13:27:23.776 1620-1943/system_process W/InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@7b87dab attribute=null, token = android.os.BinderProxy@ba60539
  18. 11-18 13:27:26.741 1317-1603/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 5574322 , only wrote 5436064
  19. 11-18 13:28:00.085 1212-1272/? D/hwcomposer: hw_composer sent 84 syncs in 60s
  20. 11-18 13:29:00.061 1212-1272/? D/hwcomposer: hw_composer sent 4 syncs in 60s
  21.  
  22. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  23.  
  24. // Creating views
  25. private EditText editTextEmail;
  26. private EditText editTextPassword;
  27. private EditText editTextPhone;
  28. private EditText editTextConfirmOtp;
  29.  
  30. private AppCompatButton buttonRegister;
  31. private AppCompatButton buttonConfirm;
  32.  
  33. //Volley RequestQueue
  34. private RequestQueue requestQueue;
  35.  
  36. //String variables to hold email, password and phone number
  37. private String username;
  38. private String password;
  39. private String phone;
  40.  
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_main);
  45.  
  46. //Initializing Views
  47. editTextEmail = (EditText) findViewById(R.id.editTextEmail);
  48. editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  49. editTextPhone = (EditText) findViewById(R.id.editTextPhone);
  50.  
  51. buttonRegister = (AppCompatButton) findViewById(R.id.buttonRegister);
  52.  
  53. //Initializing the RequestQueue
  54. requestQueue = Volley.newRequestQueue(this);
  55.  
  56. //Adding a listener to button
  57. buttonRegister.setOnClickListener((View.OnClickListener) this);
  58. }
  59.  
  60. // This method would confirm the OTP
  61.  
  62. private void confirmOtp() throws JSONException {
  63.  
  64. //Creating a Layout Inflater object for the dialog box
  65. LayoutInflater li = LayoutInflater.from(this);
  66.  
  67. //Creating a view to get the dialog box
  68.  
  69. View confirmDialog = li.inflate(R.layout.dialog_confirm, null);
  70.  
  71. // Initialing confirm button for dialog box and edittext of dialog box
  72.  
  73. buttonConfirm = (AppCompatButton) confirmDialog.findViewById(R.id.buttonConfirm);
  74. editTextConfirmOtp = (EditText) confirmDialog.findViewById(R.id.editTextOtp);
  75.  
  76. //Creating an alert dialog builder
  77. AlertDialog.Builder alert = new AlertDialog.Builder(this);
  78.  
  79. //Adding our dialog box to the view of alert dialog
  80. alert.setView(confirmDialog);
  81.  
  82. //Creating an alert dialog
  83. final AlertDialog alertDialog = alert.create();
  84.  
  85. //Displaying the alert dialog
  86. alertDialog.show();
  87.  
  88. // Onclick of the confirm button from alert dialog
  89. buttonConfirm.setOnClickListener(new View.OnClickListener() {
  90. @Override
  91. public void onClick(View v) {
  92. //Hiding the alert dialog
  93. alertDialog.dismiss();
  94.  
  95. //Displaying a progressbar
  96. final ProgressDialog loading = ProgressDialog.show(MainActivity.this, "Authenticating ", "Please wait while we check the entered code", false, false);
  97.  
  98. //Getting the user entered otp from edittext
  99. final String otp = editTextConfirmOtp.getText().toString().trim();
  100.  
  101. //Creating an string request
  102. StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.CONFIRM_URL,
  103. new Response.Listener<String>() {
  104. @Override
  105. public void onResponse(String response) {
  106. if (response.equalsIgnoreCase("success")) {
  107. loading.dismiss();
  108.  
  109. //Start a new activity
  110. startActivity(new Intent(MainActivity.this, Success.class));
  111. } else {
  112. //Displaying a toast if the otp entered is wrong
  113. Toast.makeText(MainActivity.this, "Wrong OTP Please try Again", Toast.LENGTH_LONG).show();
  114. try {
  115. //Asking user to enter otp again
  116. confirmOtp();
  117. } catch (JSONException e) {
  118. e.printStackTrace();
  119. }
  120. }
  121. }
  122. },
  123. new Response.ErrorListener() {
  124. @Override
  125. public void onErrorResponse(VolleyError error) {
  126. alertDialog.dismiss();
  127. }
  128. }){
  129.  
  130. @Override
  131. protected Map<String, String> getParams() throws AuthFailureError {
  132. Map<String, String> params = new HashMap<String, String>();
  133. //Adding the parameters otp and email
  134.  
  135. params.put(Config.KEY_OTP, otp);
  136. params.put(Config.KEY_USERNAME, username);
  137. return params;
  138. }
  139. };
  140. //Adding the request to the queue
  141. requestQueue.add(stringRequest);
  142. }
  143. });
  144. }
  145.  
  146.  
  147. //this method will get register the user
  148. private void register() {
  149. //Displaying a progress dialog
  150. final ProgressDialog loading = ProgressDialog.show(this, "Registering", "Please wait...", false, false);
  151.  
  152. //Getting user data
  153. username = editTextEmail.getText().toString().trim();
  154. password = editTextPassword.getText().toString().trim();
  155. phone = editTextPhone.getText().toString().trim();
  156.  
  157. //Again creating the request
  158. StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.REGISTER_URL,
  159. new Response.Listener<String>() {
  160. @Override
  161. public void onResponse(String response) {
  162. loading.dismiss();
  163. try {
  164. //Creating the json object from the response
  165. JSONObject jsonResponse = new JSONObject(response);
  166.  
  167. //IF it is success
  168. if (jsonResponse.getString(Config.TAG_RESPONSE).equalsIgnoreCase("success")) {
  169. //Asking user to confirm otp
  170. confirmOtp();
  171. } else {
  172. //If not successful user may already have registerd
  173. Toast.makeText(MainActivity.this, "Username or Phone Number already registered", Toast.LENGTH_LONG).show();
  174. }
  175. } catch (JSONException e) {
  176. e.printStackTrace();
  177. }
  178. }
  179. },
  180. new Response.ErrorListener() {
  181. @Override
  182. public void onErrorResponse(VolleyError error) {
  183. loading.dismiss();
  184. Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
  185.  
  186. }
  187. }) {
  188. @Override
  189. protected Map<String, String> getParams()throws AuthFailureError {
  190. Map<String, String> params = new HashMap<>();
  191. //Adding the params to the request
  192. params.put(Config.KEY_USERNAME, username);
  193. params.put(Config.KEY_PASSWORD, password);
  194. params.put(Config.KEY_PHONE, phone);
  195. return params;
  196. }
  197. };
  198.  
  199. //Adding request to the queue
  200. requestQueue.add(stringRequest);
  201. }
  202.  
  203. @Override
  204. public void onClick(View v){
  205. //calling refgister method on register button click
  206. register();
  207. }
  208. }
  209.  
  210. public class Config {
  211.  
  212. public static final String REGISTER_URL = "http://192.168.0.4/famous/register.php";
  213. public static final String CONFIRM_URL = "http://192.168.0.4/famous/confirm.php";
  214.  
  215. //Keys to send username, password, phone and otp
  216. public static final String KEY_USERNAME = "username";
  217. public static final String KEY_PASSWORD = "password";
  218. public static final String KEY_PHONE = "phone";
  219. public static final String KEY_OTP = "otp";
  220.  
  221. //JSON Tag from response from server
  222. public static final String TAG_RESPONSE= "ErrorMessage";
  223. }
  224.  
  225. dependencies {
  226. implementation fileTree(dir: 'libs', include: ['*.jar'])
  227. implementation 'com.android.support:appcompat-v7:26.1.0'
  228. implementation 'com.android.support.constraint:constraint-layout:1.0.2'
  229. testImplementation 'junit:junit:4.12'
  230. androidTestImplementation 'com.android.support.test:runner:1.0.1'
  231. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
  232.  
  233. compile 'com.android.support:design:26.1.0'
  234. compile 'com.mcxiaoke.volley:library:1.0.19'
  235. }
  236.  
  237. <?php
  238. // Constants for our API
  239. // this is applicable only when you are using SMS API
  240. define('SMSUSER', $_POST[username]);
  241. define('PASSWORD', $_POST[password]);
  242. define('PHONE', $_POST[phone]);
  243. // define('SENDERID', 'EDCRTZ');
  244.  
  245. // This function will send the otp
  246.  
  247.  
  248. $otp = int rand(100000, 999999);
  249.  
  250. // This is the sms text that will be sent via sms
  251. $sms_content = "Welcome ".SMSUSER.", to DEMO Tp RTA app: Your verification code is".$otp ;
  252.  
  253. // This is the Actual API URL concatnated with required values
  254. $api_url ="http://mymessageapi".$sms_content."&senderId=EDCRTZ&routeId=1&mobileNos=".PHONE."&smsContentType=english";
  255.  
  256.  
  257. //Envoking the API url and getting the response
  258. $response = file_get_contents($api_url);
  259.  
  260. //Returning the response
  261. return $response;
  262.  
  263. //If a past request comes to this script
  264. if ($_SERVER['REQUEST_METHOD']=='POST') {
  265. // getting username password and phone number
  266. $username = $_POST['username'];
  267. $password = $_POST['password'];
  268. $phone = $_POST['phone'];
  269.  
  270. //Generating a 6 digits OTP or verification code
  271. $otp = rand(100000, 999999);
  272.  
  273.  
  274. //Importing the db connection script
  275.  
  276. require_once('dbConnect.php');
  277.  
  278. //Creating an SQL Query
  279. $sql = "INSERT INTO famous (username, password, phone, otp) values('$username','$password','$phone','$otp')";
  280.  
  281. //If the query executed on the db successfully
  282. if (mysql_query($con,$sql)) {
  283. // printing the failure message in json
  284. echo '{"ErrorMessage":"Failure"}';
  285. }
  286. //Closing the databse connection
  287. mysqli_close($con);
  288. }
  289. ?>
  290.  
  291. <?php
  292.  
  293. //If a post request is detected
  294. if ($_SERVER['REQUEST_METHOD']=='POST') {
  295.  
  296. //gETTING THE username and otp
  297. $username = $_POST['username'];
  298. $otp = $_POST['otp'];
  299.  
  300.  
  301. // Importing the dbConnect script
  302. require_once('dbConnect.php');
  303.  
  304. //Creating an SQL to fetch the otp from the table
  305. $sql = "SELECT otp FROM test-table WHERE username = '$username'";
  306.  
  307. //Getting the result array from databse
  308. $result = mysqli_fetch_array(mysqli_query($con,$sql));
  309.  
  310. //Getting the otp from the array
  311. $realotp = $result['otp'];
  312.  
  313. //Getting the otp given is equal to otp fetched from database
  314. if ($otp == $realotp) {
  315. // Creating an sql query to update the column verified to 1 for the specified user
  316.  
  317. $sql = "UPDATE test-table SET verified= '1' WHERE username ='$username'";
  318.  
  319. //If the table is updated
  320. if (mysqli_query($con,$sql)) {
  321. //displaying failure
  322. echo 'success';
  323. }else{
  324. //displaying failure
  325. echo "failure";
  326. }
  327. }else{
  328. //displaying failure if otp is not equal to the otp fetched from databse
  329. echo 'failure';
  330. }
  331. //Closing the database
  332. mysql_close($con);
  333.  
  334. }
  335. ?>
  336.  
  337. <?php
  338. define('HOST', 'localhost');
  339. define('USER', 'root');
  340. define('PASS', '');
  341. define('DB', 'famous');
  342.  
  343. //connecting to database
  344. $con = mysqli_connect(HOST, USER, PASS, DB) or die('Unable to Connect');
  345. ?>
Add Comment
Please, Sign In to add comment