Advertisement
Guest User

Untitled

a guest
Jul 10th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. package com.example.harshitbatra.hammertime;
  2.  
  3. import android.os.AsyncTask;
  4. import android.util.Log;
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.BufferedWriter;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.OutputStream;
  12. import java.io.OutputStreamWriter;
  13. import java.io.UnsupportedEncodingException;
  14. import java.net.HttpURLConnection;
  15. import java.net.MalformedURLException;
  16. import java.net.URL;
  17. import java.net.URLEncoder;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20.  
  21. import javax.net.ssl.HttpsURLConnection;
  22.  
  23. /**
  24. * Created by Harshit Batra on 06-07-2016.
  25. */
  26. public class Signupasync extends AsyncTask<String, Void, String>
  27. {
  28. String username,phone,address,email,password;
  29. String url;
  30.  
  31. private postexecutelistener myListener;
  32.  
  33. public Signupasync(postexecutelistener pel,String url,String username, String phone,String address,String email,String password )
  34. {
  35. myListener = pel;
  36. this.url = url;
  37. this.username = username;
  38. this.phone = phone;
  39. this.address = address;
  40. this.email = email;
  41. this.password = password;
  42. }
  43.  
  44. public interface postexecutelistener
  45. {
  46. public void postexecutedone(String str);
  47. }
  48.  
  49. public String connect()
  50. {
  51. HashMap<String,String> hash = new HashMap<>(6);
  52. hash.put("usernamekey",username);
  53. hash.put("phonekey",phone);
  54. hash.put("addresskey",address);
  55. hash.put("emailkey",email);
  56. hash.put("passwordkey",password);
  57.  
  58. return performPostCall(url,hash);
  59.  
  60. }
  61.  
  62. public String performPostCall(String requestURL, HashMap<String, String> postDataParams)
  63. {
  64.  
  65. URL url;
  66. String response = "";
  67. try
  68. {
  69. url = new URL(requestURL);
  70.  
  71. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  72. conn.setReadTimeout(15000);
  73. conn.setConnectTimeout(15000);
  74. conn.setRequestMethod("POST");
  75. conn.setDoInput(true);
  76. conn.setDoOutput(true);
  77.  
  78.  
  79. OutputStream os = conn.getOutputStream();
  80. BufferedWriter writer = new BufferedWriter(
  81. new OutputStreamWriter(os, "UTF-8"));
  82. writer.write(getPostDataString(postDataParams));
  83.  
  84. writer.flush();
  85. writer.close();
  86. os.close();
  87. int responseCode = conn.getResponseCode();
  88.  
  89. if (responseCode == HttpsURLConnection.HTTP_OK)
  90. {
  91. String line;
  92. BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  93. while ((line = br.readLine()) != null)
  94. {
  95. response += line;
  96. }
  97. }
  98. else
  99. {
  100. response = "";
  101.  
  102. }
  103. }
  104. catch (Exception e)
  105. {
  106. e.printStackTrace();
  107. }
  108.  
  109. return response;
  110. }
  111. private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException
  112. {
  113. StringBuilder result = new StringBuilder();
  114. boolean first = true;
  115. for (Map.Entry<String, String> entry : params.entrySet())
  116. {
  117. if (first)
  118. first = false;
  119. else
  120. result.append("&");
  121.  
  122. result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
  123. result.append("=");
  124. result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
  125. }
  126. Log.e("dsasd", "getPostDataString: " + result);
  127. return result.toString();
  128. }
  129.  
  130.  
  131. @Override
  132. protected String doInBackground(String... para)
  133. {
  134. return connect();
  135. }
  136.  
  137. @Override
  138. protected void onPostExecute(String str)
  139. {
  140. super.onPostExecute(str);
  141. Log.d("jsdk", "String from signup.php " + str);
  142. myListener.postexecutedone(str);
  143. }
  144.  
  145. }
  146.  
  147. $con = mysqli_connect("mysql.hostinger.in","u626717580_batra","hb99960","u626717580_mydb");
  148.  
  149.  
  150. if($con)
  151. {
  152.  
  153. $username = $_POST['usernamekey'];
  154. $phone = $_POST['phonekey'];
  155. $address = $_POST['addresskey'];
  156. $email = $_POST['emailkey'];
  157. $password = $_POST['passwordkey'];
  158.  
  159.  
  160. $sql = "INSERT INTO `u626717580_mydb`.`signup` (
  161. `username` ,
  162. `phone` ,
  163. `address` ,
  164. `email` ,
  165. `password`
  166. )
  167. VALUES (
  168. '$username', '$phone', '$address', '$email', '$password'
  169. );
  170. ";
  171.  
  172. if($username)
  173. {
  174. if(mysqli_query($con,$sql) )
  175. echo "true";
  176. else
  177. echo "false";
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement