我是Android的新手。
我希望在用户登录时将用户ID存储在MySQL数据库的全局变量中。我已经检查了现有的登录详细信息。现在我想在全局变量中获取该ID。
这是我的代码:
用户登陆
@Override
public void onClick(View view) {
String email = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = "login";
new BackgroundWorker().execute(type,email,password);
//finish();
Intent intent = new Intent(UserLogin.this, MainActivity.class);
startActivity(intent);
finish();
}
class BackgroundWorker extends AsyncTask<String, String, String>{
//String result = "";
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://www.mybusket.com/pmsapp/webs/get_all_emp.php";
try {
String email_id = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("email_id", "UTF-8")+"="+URLEncoder.encode(email_id, "UTF-8")+"&"
+URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String line="";
String result="";
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine())!= null) {
sb.append(line + "\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
//result = sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
/*try {
//GlobalVariable id;
JSONObject jsonObject = new JSONObject(result);
JSONArray array = jsonObject.getJSONArray("emp");
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
GlobalVariable g = new GlobalVariable();
g.setId(obj.getInt("empid"));
}
}catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
//e.printStackTrace();
}*/
}
}
我想将该员工ID存储在全局变量中。我怎么能这样做?