为什么我的android标签没有从我的php脚本收到?
这是我的Login.java为什么我的android标签没有从我的php脚本收到?,php,android,json,Php,Android,Json,这是我的Login.java public class Login extends ActionBarActivity implements OnClickListener { private Button login, register; private EditText email, password; private JSONArray loginposition = null; // Progress Dialog private Progre
public class Login extends ActionBarActivity implements OnClickListener {
private Button login, register;
private EditText email, password;
private JSONArray loginposition = null;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.1.10:1234/PMSS/login.php";
// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_POSTS = "posts";
private static final String TAG_POSITION = "position";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.registerlauncher);
email = (EditText) findViewById(R.id.userid);
password = (EditText) findViewById(R.id.password);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String Username = email.getText().toString();
String Password = password.getText().toString();
new AttemptLogin(Username, Password).execute();
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Login.this, Register.class);
startActivity(intent);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// For the main activity, make sure the app icon in the action bar
// does not behave as a button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
/*
* case R.id.login: new AttemptLogin().execute(); break; case
* R.id.register: Intent i = new Intent(Login.this, Register.class);
* startActivity(i); break;
*/
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, Integer> {
private final String TAG = null;
boolean failure = false;
String res;
String Username;
String Password;
String Position;
int success;
public AttemptLogin(String Username, String Password) {
this.Username = Username;
this.Password = Password;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected Integer doInBackground(String... args) {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", 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);
res = json.getString(TAG_MESSAGE);
loginposition = json.getJSONArray(TAG_POSTS);
// // looping through all posts according to the json object
// // returned
for (int i = 0; i < loginposition.length(); i++) {
JSONObject c = loginposition.getJSONObject(i);
// gets the content of each tag
// String content = c.getString(TAG_USERID);
Position = c.getString(TAG_POSITION);
}
} catch (JSONException e) {
Log.e(TAG, "JSON error", e);
success = Integer.valueOf(0);
}
return success;
}
protected void onPostExecute(Integer success) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (success != null && success == 1) {
Log.d("Login Successful!", "res: " + res);
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Login.this);
Editor edit = sp.edit();
edit.putString("email", Username);
edit.commit();
if(Position != "user"){
Intent user = new Intent(Login.this, MainMenu.class);
startActivity(user);
}
else if(Position != "staff"){
Intent staff = new Intent(Login.this, StaffMainMenu.class);
startActivity(staff);
}
Toast.makeText(
Login.this,
res == null ? "Please enter both user id and password (success)"
: res, Toast.LENGTH_LONG).show();
email.setText(null);
password.setText(null);
} else {
Log.d("Login Failure!", "res: " + res);
Toast.makeText(
Login.this,
res == null ? "Please enter both user id and password (failed)"
: res, Toast.LENGTH_LONG).show();
}
}
}
}
我的代码既没有编译错误,也没有运行时错误
这是我点击登录按钮后的日志猫
12-13 15:25:22.009: D/request!(26763): starting
12-13 15:25:22.309: D/Login attempt(26763): {"posts":["user"],"message":"Login Successful!","success":1}
12-13 15:25:22.309: E/(26763): JSON error
12-13 15:25:22.309: E/(26763): org.json.JSONException: Value user at 0 of type java.lang.String cannot be converted to JSONObject
12-13 15:25:22.309: E/(26763): at org.json.JSON.typeMismatch(JSON.java:96)
12-13 15:25:22.309: E/(26763): at org.json.JSONArray.getJSONObject(JSONArray.java:484)
12-13 15:25:22.309: E/(26763): at com.pmss.Login$AttemptLogin.doInBackground(Login.java:157)
12-13 15:25:22.309: E/(26763): at com.pmss.Login$AttemptLogin.doInBackground(Login.java:1)
12-13 15:25:22.309: E/(26763): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-13 15:25:22.309: E/(26763): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-13 15:25:22.309: E/(26763): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-13 15:25:22.309: E/(26763): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-13 15:25:22.309: E/(26763): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-13 15:25:22.309: E/(26763): at java.lang.Thread.run(Thread.java:1019)
12-13 15:25:22.319: D/Login Failure!(26763): res: Login Successful!
我编辑了我的java代码,并将我的logcat发布在我的question@Joop艾根,请帮帮我
if(Position == "user"){
Intent user = new Intent(Login.this, MainMenu.class);
startActivity(user);
}
else if(Position == "staff"){
Intent staff = new Intent(Login.this, StaffMainMenu.class);
startActivity(staff);
}
12-13 15:25:22.009: D/request!(26763): starting
12-13 15:25:22.309: D/Login attempt(26763): {"posts":["user"],"message":"Login Successful!","success":1}
12-13 15:25:22.309: E/(26763): JSON error
12-13 15:25:22.309: E/(26763): org.json.JSONException: Value user at 0 of type java.lang.String cannot be converted to JSONObject
12-13 15:25:22.309: E/(26763): at org.json.JSON.typeMismatch(JSON.java:96)
12-13 15:25:22.309: E/(26763): at org.json.JSONArray.getJSONObject(JSONArray.java:484)
12-13 15:25:22.309: E/(26763): at com.pmss.Login$AttemptLogin.doInBackground(Login.java:157)
12-13 15:25:22.309: E/(26763): at com.pmss.Login$AttemptLogin.doInBackground(Login.java:1)
12-13 15:25:22.309: E/(26763): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-13 15:25:22.309: E/(26763): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-13 15:25:22.309: E/(26763): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-13 15:25:22.309: E/(26763): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-13 15:25:22.309: E/(26763): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-13 15:25:22.309: E/(26763): at java.lang.Thread.run(Thread.java:1019)
12-13 15:25:22.319: D/Login Failure!(26763): res: Login Successful!