我正在尝试使用json将数据发布到mysql中。我还使用JSON登录功能。登录工作完全正常。问题出在加载活动上。在我插入所有数据并单击“保存”按钮后,它将我带回到之前的活动并说“登录成功”,这应该只发生在登录活动上。我检查了所有的布局,以防我引用了错误的按钮进行保存,但一切都按预期的方式进行。
Login.java
package com.example.bsc.barloworld;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Login extends Activity implements OnClickListener {
private EditText user, pass;
private Button mSubmit;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.2.101/webservice/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// setup input fields
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
Button b2 = (Button) findViewById(R.id.button2);
// setup buttons
mSubmit = (Button) findViewById(R.id.login);
// register listeners
mSubmit.setOnClickListener(this);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("System Attempting Login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("User_id", username));
params.add(new BasicNameValuePair("Password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Login.this);
SharedPreferences.Editor edit = sp.edit();
edit.putString("User_id", username);
edit.commit();
Intent i = new Intent(Login.this, Display_Menu.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
package com.example.bsc.barloworld;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.view.View.OnClickListener;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Loading extends Activity implements OnClickListener {
JSONParser jsonParser = new JSONParser();
public String[] SugarType;
public int position =0;
private EditText Units,FromWareHouse,ToWareHouse,DeliveryNumber,Transporter,NumberPlate;
private DatePicker dp;
private Button bSave;
private ProgressDialog pDialog;
private static final String POST_COMMENT_URL = "http://192.168.2.101/webservice/adddispatch.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading);
bSave = (Button) findViewById(R.id.SaveDispatch);
bSave.setOnClickListener(this);
Units = (EditText)findViewById(R.id.Units);
FromWareHouse = (EditText)findViewById(R.id.FromWarehouse);
ToWareHouse = (EditText)findViewById(R.id.ToWarehouse);
dp = (DatePicker) findViewById(R.id.Date);
SugarType = getResources().getStringArray(R.array.TypeofSugar);
Spinner spSugarType = (Spinner) findViewById(R.id.spinnerSugarType);
Transporter = (EditText)findViewById(R.id.Transporter);
DeliveryNumber = (EditText)findViewById(R.id.DeliveryNumber);
NumberPlate = (EditText)findViewById(R.id.NumberPlate);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, SugarType);
spSugarType.setAdapter(adapter);
//setting up listener for the spinner
spSugarType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
position = index;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
@Override
public void onClick(View v) {
new AddLoad().execute();
}
class AddLoad extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Loading.this);
pDialog.setMessage("Adding Load...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String strSugarType = SugarType[position];;
String strFromWareHouse = FromWareHouse.getText().toString();
String strToWareHouse = ToWareHouse.getText().toString();
String strUnits = Units.getText().toString();
String strDate = dp.getDayOfMonth() + "/"
+ (dp.getMonth() + 1) + "/" + dp.getYear();
String strDeliveryNumber = DeliveryNumber.getText().toString();
String strTransporter = Transporter.getText().toString();
String strNumberPlate = NumberPlate.getText().toString();
//String post_dayt = dayt.getText().toString();
//shared preferences store data about the current logged in user
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Loading.this);
String post_username = sp.getString("User_id", "");
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("User_id", post_username));
params.add(new BasicNameValuePair("SugarType", strSugarType));
params.add(new BasicNameValuePair("Units", strUnits));
params.add(new BasicNameValuePair("FromWarehouse", strFromWareHouse));
params.add(new BasicNameValuePair("ToWarehouse", strToWareHouse));
params.add(new BasicNameValuePair("Date", strDate));
params.add(new BasicNameValuePair("DeliveryNumber", strDeliveryNumber));
params.add(new BasicNameValuePair("Transporter", strTransporter));
params.add(new BasicNameValuePair("NumberPlate", strNumberPlate));
Log.d("request!", "starting");
//Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(
POST_COMMENT_URL, "POST", params);
// full json response
Log.d("Post Comment attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Comment Added!", json.toString());
finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Comment Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Loading.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}