Advertisement
Guest User

Untitled

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