-
Notifications
You must be signed in to change notification settings - Fork 0
/
Login.java
133 lines (87 loc) · 3.23 KB
/
Login.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.d_andaman;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
public class Login extends AsyncTask<String,Void,JSONObject>{
private Context context;
private int byGetOrPost = 0;
public Login(Context context,int flag) {
this.context = context;
byGetOrPost = flag;
}
protected void onPreExecute(){
}
@Override
protected JSONObject doInBackground(String... arg0) {
if(byGetOrPost == 0){ //means by Get Method
try{
String phone = (String)arg0[0];
String link = "http:https://127.0.0.1/login.php?phone="+phone;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line="";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
}catch(Exception e){return null;}
}
else{
try{
String phone = (String)arg0[0];
String link="http:https://192.168.83.1/dand_api/check.php";
String data = URLEncoder.encode("phone", "UTF-8") + "=" + URLEncoder.encode(phone, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return new JSONObject(sb.toString());
}
catch(Exception e){e.printStackTrace();}
}
return null;
}
@Override
protected void onPostExecute(JSONObject result){
int i=convertjson(result);
MainActivity.setid(i);
}
private int convertjson(JSONObject result) {
try{
JSONArray id=result.getJSONArray("id");
JSONObject u=id.getJSONObject(0);
return u.getInt("id");
}catch(JSONException e){e.printStackTrace(); }
return 0;
}
}