我在login.java中有两个活动,比如login.java和sync.java,我有两个编辑文本字段,即' id'和密码'和一个登录按钮,在sync.java我有一个名为sync的按钮,所以我想要做的是保存登录活动' id'单击同步活动按钮后数据库中的字段。 P.S:我正在制作一个Android应用程序并使用MySQL服务器来保存数据。
这是login.java
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.registerLink:
Intent registerIntent = new Intent(login.this, register.class);
startActivity(registerIntent);
break;
case R.id.bLogin:
if (etUserId.getText().length() == 0 || etPassword.getText().length() == 0) {
etUserId.setError( "Fill in the field" );
etPassword.setError("Fill in the field");}
else{
id = etUserId.getText().toString();
password = etPassword.getText().toString();
String method = "login";
backgroundTask bTask = new backgroundTask(this);
bTask.execute(method, id, password);
}
这是sync.java
@Override
public void onClick(View v)
{
String method = "sync";
backgroundTask bTask = new backgroundTask(this);
bTask.execute(method);
//startActivity(new Intent(this,synchronization.class));
}
这是数据库内容正在进行的background.java
@Override
protected String doInBackground(String... params) {
String reg_url = "http://10.0.2.2/app/register.php";
String login_url = "http://10.0.2.2/app/login.php";
String sync_url = "http://10.0.2.2/app/sync.php";
//192.168.10.9
if(method.equals("login"))
{
String id = params[1];
String password = params[2];
try {
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 data = URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("sync")) {
try {
URL url = new URL(sync_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OS = httpURLConnection.getOutputStream();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line;
}
bufferedReader.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
//httpURLConnection.connect();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals("login successful"))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
ctx.startActivity(new Intent(ctx, sync.class));
}
else{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
if(result.equals("sync successful"))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
ctx.startActivity(new Intent(ctx, synchronization.class));
}
} }