由于我是应用程序开发的新手,我使用JSON使用PHP连接到我的数据库,但是我遇到了以下代码的错误:
public class MainActivity extends Activity {
EditText etemail, etpass;
Button login, register;
TextView tError;
ProgressDialog pDialog;
//TextView PToDoc;
private static String KEY_SUCCESS = "success";
//private static String KEY_ERROR = "error";
private static final String KEY_REFERRED_AS = "referred_as";
private static final String KEY_EMAIL = "email";
private static final String KEY_CARD = "card";
private static final String KEY_ADDRESS = "address";
private static final String KEY_CONTACT_NO = "contact_no";
private static final String KEY_ID = "id";
private static final String KEY_DATEOFBIRTH = "dateofbirth";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setting default screen to sign_in.xml
setContentView(R.layout.activity_main);
etemail = (EditText) findViewById(R.id.emailAddress);
etpass = (EditText) findViewById(R.id.password);
tError = (TextView) findViewById(R.id.error);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.reg);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
String email = etemail.getText().toString();
String password = etpass.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(email, password);
try {
if (json.getString(KEY_SUCCESS) == null) {
tError.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_REFERRED_AS),json_user.getString(KEY_EMAIL),json_user.getString(KEY_CARD),
json_user.getString(KEY_DATEOFBIRTH),json_user.getString(KEY_ADDRESS), json_user.getString(KEY_CONTACT_NO),json_user.getString(KEY_ID));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), Home.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
tError.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),Register.class);
startActivity(i);
}
});
}
}
答案 0 :(得分:0)
在login按钮的click事件中获取电子邮件和密码并将其添加到JSONObject,然后将其传递给asyn类以将其发送到特定的weblink 相关代码如下:
class SendAsyn extends AsyncTask<JSONObject, JSONObject, JSONObject> {
String url = "http://Yourweblink.com/Yourdbfile.php";
Boolean repeat=true;
@Override
protected JSONObject doInBackground(JSONObject... params) {
// TODO Auto-generated method stub
JSONObject json = params[0];
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 100000);
String resFromServer="null";
JSONObject jsonResponse = null;
HttpPost post = new HttpPost(url);
try {
StringEntity se = new StringEntity("regis=" + json.toString());
post.addHeader("content-type", "application/x-www-form-urlencoded");
post.setEntity(se);
HttpResponse response;
Log.i("URL",post.getParams().toString());
response = client.execute(post);
resFromServer = org.apache.http.util.EntityUtils
.toString(response.getEntity());
if(resFromServer.trim().equals("successfull")){
LoginActivity.suc=true;
LoginActivity.error=false;
Log.i("here we are","successfull"+resFromServer);
}else{
String[] tokens=resFromServer.split("~");
Log.i("here we are else",resFromServer);
if(tokens[0].equals("Error")){
Log.i("here we are error",tokens[1]);
LoginActivity.error=true;
LoginActivity.suc=false;
}
}
//Log.i("Response", resFromServer);
} catch (Exception e) {
e.printStackTrace();
}
return jsonResponse;
}
}
使用
调用此类 SendAsyn transmitter = new SendAsyn();
transmitter.execute(new JSONObject[]{Your JSONObject});