Advertisement
Guest User

Untitled

a guest
May 12th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.25 KB | None | 0 0
  1. <?php
  2. include_once './connect_db.php';
  3. $db = new DBConnect();
  4. $response = array();
  5.  
  6. $username = $_POST["username"];
  7. $password = $_POST["password"];
  8.  
  9. if (empty($_POST['username']) || empty($_POST['password']))
  10. {
  11. $response["success"] = 0;
  12. $response["message"] = "One or both of the fields are empty.";
  13. die(json_encode($response));
  14. }
  15. $query = " SELECT * FROM account WHERE username = '$username'and password='$password'";
  16. $sql1 = mysql_query($query);
  17. $row = mysql_fetch_array($sql1);
  18. if (!empty($row))
  19. {
  20. $response["success"] = 1;
  21. $response["message"] = "You have been sucessfully login";
  22. die(json_encode($response));
  23. }
  24. else
  25. {
  26. $response["success"] = 0;
  27. $response["message"] = "invalid username or password ";
  28. die(json_encode($response));
  29. }
  30. mysql_close();
  31. ?>
  32.  
  33. package com.example.rossh.register;
  34.  
  35. import java.util.ArrayList;
  36. import java.util.List;
  37.  
  38. import org.apache.http.NameValuePair;
  39. import org.apache.http.message.BasicNameValuePair;
  40. import org.json.JSONException;
  41. import org.json.JSONObject;
  42.  
  43. import android.app.Activity;
  44. import android.app.ProgressDialog;
  45. import android.content.Context;
  46. import android.content.Intent;
  47. import android.content.SharedPreferences;
  48. import android.os.AsyncTask;
  49. import android.os.Bundle;
  50. import android.support.v7.app.AppCompatActivity;
  51. import android.support.v7.widget.Toolbar;
  52. import android.text.TextUtils;
  53. import android.util.Log;
  54. import android.view.View;
  55. import android.view.View.OnClickListener;
  56. import android.widget.Button;
  57. import android.widget.EditText;
  58. import android.widget.Toast;
  59.  
  60. public class login extends AppCompatActivity implements OnClickListener {
  61. private EditText user, pass;
  62. private Button login;
  63. JSONObject jsonObject;
  64. String response;
  65. // Progress Dialog
  66. private ProgressDialog pDialog;
  67. private static final String TAG_SUCCESS = "success";
  68. private static final String TAG_MESSAGE = "message";
  69.  
  70. @Override
  71. protected void onCreate(Bundle savedInstanceState) {
  72. // TODO Auto-generated method stub
  73. super.onCreate(savedInstanceState);
  74. setContentView(R.layout.activity_login);
  75. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  76. setSupportActionBar(toolbar);
  77.  
  78. //setup input fields
  79. user = (EditText) findViewById(R.id.username);
  80. pass = (EditText) findViewById(R.id.password);
  81.  
  82. //setup buttons
  83. login = (Button) findViewById(R.id.btnlogin);
  84.  
  85. //register listeners
  86. login.setOnClickListener(this);
  87. }
  88.  
  89. @Override
  90. public void onClick(View v) {
  91. // TODO Auto-generated method stub
  92. switch (v.getId()) {
  93. case R.id.btnlogin:
  94. String username = user.getText().toString();
  95. String password = pass.getText().toString();
  96. boolean cancel =false;
  97. View focusView = null;
  98. if(TextUtils.isEmpty(username))
  99. {
  100. user.setError("This field is required!!");
  101. cancel=true;
  102. focusView=user;
  103. }
  104. if(TextUtils.isEmpty(password))
  105. {
  106. pass.setError("This field is requird!!!");
  107. cancel=true;
  108. focusView=pass;
  109. }
  110. if(cancel)
  111. {
  112. focusView.requestFocus();
  113. } else {
  114. new AttemptLogin().execute(username, password);
  115. }
  116. break;
  117.  
  118. default:
  119. break;
  120. }
  121. }
  122.  
  123. class AttemptLogin extends AsyncTask<String, String, String> {
  124. /**
  125. * Before starting background thread Show Progress Dialog
  126. */
  127. boolean failure = false;
  128.  
  129. @Override
  130. protected void onPreExecute() {
  131. super.onPreExecute();
  132. pDialog = new ProgressDialog(login.this);
  133. pDialog.setMessage("Attempting login...");
  134. pDialog.show();
  135. }
  136.  
  137. @Override
  138. protected String doInBackground(String... args) {
  139. // TODO Auto-generated method stub
  140. // Check for success tag
  141. String username = args[0];
  142. String password = args[1];
  143. // Building Parameters
  144. final int success;
  145. List<NameValuePair> params = new ArrayList<NameValuePair>();
  146. params.add(new BasicNameValuePair("username", username));
  147. params.add(new BasicNameValuePair("password", password));
  148.  
  149. Log.d("request!", "starting");
  150. // getting product details by making HTTP request
  151. JsonParser jsonParser = new JsonParser();
  152.  
  153. jsonObject = jsonParser.makeHttpRequest(AppConfig.LOGIN_URL, "POST", params);
  154. if (jsonObject != null) {
  155. try{
  156. Log.d("Json data",">"+jsonObject);
  157. success = jsonObject.getInt(TAG_SUCCESS);
  158.  
  159. if (success == 1) {
  160. response = jsonObject.getString(TAG_MESSAGE);
  161. } else {
  162. Log.d("Login Failure!", jsonObject.getString(TAG_MESSAGE));
  163. response = jsonObject.getString(TAG_MESSAGE);
  164. }
  165. } catch(JSONException e) {
  166. e.printStackTrace();
  167. }
  168. }
  169. return response;
  170. }
  171.  
  172. /**
  173. * After completing background task Dismiss the progress dialog
  174. **/
  175. protected void onPostExecute(String file_url) {
  176. // dismiss the dialog once product deleted
  177. pDialog.dismiss();
  178. if (file_url != null) {
  179. Toast.makeText(login.this, "ok", Toast.LENGTH_LONG).show();
  180. }
  181. }
  182. }
  183. }
  184. }
  185.  
  186. package com.example.rossh.register;
  187.  
  188. import android.util.Log;
  189. import java.io.BufferedReader;
  190. import java.io.IOException;
  191. import java.io.InputStream;
  192. import java.io.InputStreamReader;
  193. import java.io.UnsupportedEncodingException;
  194. import java.util.List;
  195.  
  196. import org.apache.http.HttpEntity;
  197. import org.apache.http.HttpResponse;
  198. import org.apache.http.NameValuePair;
  199. import org.apache.http.client.ClientProtocolException;
  200. import org.apache.http.client.entity.UrlEncodedFormEntity;
  201. import org.apache.http.client.methods.HttpGet;
  202. import org.apache.http.client.methods.HttpPost;
  203. import org.apache.http.client.utils.URLEncodedUtils;
  204. import org.apache.http.impl.client.DefaultHttpClient;
  205. import org.apache.http.protocol.HTTP;
  206. import org.json.JSONArray;
  207. import org.json.JSONException;
  208. import org.json.JSONObject;
  209.  
  210. import android.util.Log;
  211. import android.widget.Toast;
  212.  
  213. public class JsonParser {
  214. InputStream is = null;
  215. static String json = "";
  216. static JSONObject jObj = null;
  217.  
  218. // constructor
  219. public JsonParser() {
  220. }
  221.  
  222. // function get json from url
  223. // by making HTTP POST or GET mehtod
  224. public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
  225. // Making HTTP request
  226. try {
  227. // check for request method
  228. if(method == "POST"){
  229. // request method is POST
  230. // defaultHttpClient
  231. DefaultHttpClient httpClient = new DefaultHttpClient();
  232. HttpPost httpPost = new HttpPost(url);
  233. httpPost.setEntity(new UrlEncodedFormEntity(params));
  234. httpPost.setHeader("Content-Type","application/json");
  235.  
  236. HttpResponse httpResponse = httpClient.execute(httpPost);
  237. HttpEntity httpEntity = httpResponse.getEntity();
  238. is = httpEntity.getContent();
  239. }else if(method == "GET"){
  240. // request method is GET
  241. DefaultHttpClient httpClient = new DefaultHttpClient();
  242. String paramString = URLEncodedUtils.format(params, "utf-8");
  243. url += "?" + paramString;
  244. HttpGet httpGet = new HttpGet(url);
  245.  
  246. HttpResponse httpResponse = httpClient.execute(httpGet);
  247. HttpEntity httpEntity = httpResponse.getEntity();
  248. is = httpEntity.getContent();
  249. }
  250. } catch (UnsupportedEncodingException e) {
  251. e.printStackTrace();
  252. } catch (ClientProtocolException e) {
  253. e.printStackTrace();
  254. } catch (IOException e) {
  255. e.printStackTrace();
  256. }
  257.  
  258. try {
  259. BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8);
  260. StringBuilder sb = new StringBuilder();
  261. String line = null;
  262. while ((line = reader.readLine()) != null) {
  263. sb.append(line + "n");
  264. }
  265. is.close();
  266. json = sb.toString();
  267. Log.d("String",">"+json);
  268. } catch (Exception e) {
  269. Log.e("Buffer Error", "Error converting result " + e.toString());
  270. }
  271. // try parse the string to a JSON object
  272. try {
  273. JSONArray jsonarray = new JSONArray(json);
  274. jObj = jsonarray.getJSONObject(0);
  275. } catch (JSONException e) {
  276. Log.e("JSON Parser", "Error parsing " + e.toString());
  277. }
  278.  
  279. // return JSON String
  280. return jObj;
  281. }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement