我正在设置登录片段。如何使用nameValuepair获取剩余的jsonObject。然后,在TextView中显示或使用SharedPreferences将jsonObject共享给其他Fragment。
这是我的php select语句。
" SELECT student_name,student_email,student_phone,student_id,student_pw 来自学生在哪里 student_id =' $ student_id' AND student_pw =' $ student_pw'";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_loging_fragment, container,
false);
userName = (EditText) view.findViewById(R.id.editText1);
password = (EditText) view.findViewById(R.id.editText2);
btnLogin = (Button) view.findViewById(R.id.btnLogin);
textView = (TextView) view.findViewById(R.id.txtTest);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
new Thread(new Runnable() {
public void run() {
login();
passData();
}
}).start();
}
});
return view;
}
public void passData(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
JSONObject json = getData(url);
try{
JSONObject userEmail = json.getJSONObject("student_email");
JSONObject userPhone = json.getJSONObject("student_phone");
Log.v("myLog", "[USEREMAIL ] : " + userEmail.toString());
Log.v("myLog", "[USERPHONE ] : " + userPhone.toString());
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static JSONObject getData(String url){
JSONObject jArray = null;
try
{
httpclient = new DefaultHttpClient();
httppost = new HttpPost(url);
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("student_id", userName
.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("student_pw", password
.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost,
responseHandler);
Log.v("myLog", " [ PASSDATA ]");
}
catch(Exception e)
{
Log.v("myLog", "[Network Error] :" + e.toString());
}
return jArray;
}
public void login() {
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost(url);
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("student_id", userName
.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("student_pw", password
.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost,
responseHandler);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction()
.setCustomAnimations(R.anim.gla_there_come,
R.anim.gla_there_gone);
getActivity().runOnUiThread(new Runnable() {
public void run() {
textView.setText("Result : " + response);
Log.v("myLog", "[RESPONSE] :" + response);
}
});
if (response.equalsIgnoreCase("User Found")) {
getActivity().runOnUiThread(new Runnable() {
public void run() {
Log.v("myLog", "[LOGIN] :" + " SUCCESS");
}
});
UserProfileFragment userProfile = new UserProfileFragment();
ft.replace(R.id.content_frame, userProfile).commit();
SharedPreferences settings = this.getActivity()
.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("hasLoggedIn", false);
editor.putString("nameIdentify", userName.getText().toString());
editor.putString("pwIdentify", password.getText().toString());
editor.commit();
Log.v("myLog", "[PUT USERNAME] :" + userName.getText().toString());
Log.v("myLog", "[PUT USERPESS] :" + password.getText().toString());
Log.v("myLog", "[hasLoggedIn ]: false");
} else if (response.equalsIgnoreCase("No Such User Found")) {
Log.v("myLog", "[LOGIN] :" + " FAIL");
}
} catch (Exception e) {
Log.v("myLog", "[ERROR] :" + e.toString());
}
}