我得到了正确的答案,但仍然遇到错误而且很奇怪。
我按下按钮进入下一个活动:
private void setupClickEvent() {
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userName = username.getText().toString();
userPass = password.getText().toString();
if (!userName.isEmpty() || !userPass.isEmpty()) {
accessWebService();
Log.e("after execute: ", String.valueOf(letLogin));
if (letLogin) {
session.createLoginSession(userName, "waiterx@gmail.com");
Intent intent = new Intent(getActivity().getApplicationContext(), UserProfile.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
getActivity().finish();
} else {
Snackbar.with(getActivity().getApplicationContext()).text(getString(R.string.uname_or_password_not_found)).color(Color.parseColor("#3399FF")).show(getActivity());
}
}
}
});
}
private void accessWebService() {
task = new MyInsertDataTask();
task.execute(new String[]{URL});
}
更新的JAVA:
private class MyInsertDataTask extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity().getApplicationContext());
pDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminate(true);
pDialog.setMessage(getString(R.string.dialog_rate_data_submit));
pDialog.setCancelable(false);
pDialog.setInverseBackgroundForced(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(String... params) {
nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("user", userName));
nameValuePairs.add(new BasicNameValuePair("pass", userPass));
try
{
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(params[0]);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httpPost);
httpEntity = response.getEntity();
is = httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder result = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
result.append(line);
}
Log.e("HTTP response: ", result.toString());
if (result != null){
data = new JSONObject(result.toString());
isRegistered = data.getBoolean("res");}
Log.e("isRegistered: ", String.valueOf(isRegistered));
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
}
return isRegistered;
}
@Override
protected void onPostExecute(Boolean aVoid) {
super.onPostExecute(aVoid);
pDialog.dismiss();
}
}
仍然没有登录我!!!!
logcat输出:
09-23 04:36:48.627 22163-22163/com.order.app.order E/after execute:﹕ false
09-23 04:36:48.659 22163-22182/com.order.app.order W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-23 04:36:48.659 22163-22182/com.order.app.order W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xabc9aa60, error=EGL_SUCCESS
09-23 04:36:51.240 22163-22188/com.order.app.order E/HTTP response:﹕ {"res":true}
09-23 04:36:51.240 22163-22188/com.order.app.order E/isRegistered:﹕ true
09-23 04:36:53.777 22163-22163/com.order.app.order E/after execute:﹕ true
09-23 04:36:53.812 22163-22182/com.order.app.order W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-23 04:36:53.812 22163-22182/com.order.app.order W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa1d555c0, error=EGL_SUCCESS
09-23 04:36:53.815 22163-22185/com.order.app.order E/HTTP response:﹕ {"res":true}
09-23 04:36:53.815 22163-22185/com.order.app.order E/isRegistered:﹕ true
由于响应是真的,我给isRegistered
数据对象的值,为什么它仍然困扰我?第一次单击按钮时,letLogin变量为false,第二次为true。这怎么可能?任何人都能帮助我欣赏它!
答案 0 :(得分:0)
你只需将json数组转换为object,解析它。而不是
if (result != null){
data = new JSONObject(result);
if (data != null){
isRegistered = data.getBoolean("res");
} }
使用此代码
if (result != null){
jArray = new JSONArray(result.toString());
JSONObject data = jArray.getJSONObject(0); //first data, if multiple data, use loop
isRegistered = data.getBoolean("res");}
如果您有多个数据,请在内部使用循环。
答案 1 :(得分:0)
我认为你在创建JsonObject时忘记了一个toString。
尝试使用它:
data = new JSONObject(result.toString);
而不是:
data = new JSONObject(result);
无论如何都要尝试一步一步的调试
答案 2 :(得分:0)
好的,我终于找到了它为什么需要2次按下按钮。 1执行,1确认正确的数据。
代码:
private class MyInsertDataTask extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity().getApplicationContext());
pDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminate(true);
pDialog.setMessage(getString(R.string.dialog_rate_data_submit));
pDialog.setCancelable(false);
pDialog.setInverseBackgroundForced(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(String... params) {
nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("user", userName));
nameValuePairs.add(new BasicNameValuePair("pass", userPass));
try {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(params[0]);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httpPost);
httpEntity = response.getEntity();
is = httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder result = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
result.append(line);
}
Log.e("HTTP response: ", result.toString());
if (result != null) {
data = new JSONObject(result.toString());
isRegistered = data.getBoolean("res");
}
Log.e("isRegistered: ", String.valueOf(isRegistered));
} catch (Exception e) {
Log.e("Fail 1", e.toString());
}
return isRegistered;
}
@Override
protected void onPostExecute(Boolean aVoid) {
super.onPostExecute(aVoid);
pDialog.dismiss();
if (aVoid) {
session.createLoginSession(userName, "waiterx@gmail.com");
Intent intent = new Intent(getActivity().getApplicationContext(), UserProfile.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
getActivity().finish();
}else {
Snackbar.with(getActivity().getApplicationContext()).text(getString(R.string.uname_or_password_not_found)).color(Color.parseColor("#3399FF")).show(getActivity());
}
}
}
执行:
private void setupClickEvent() {
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userName = username.getText().toString();
userPass = password.getText().toString();
if (!userName.isEmpty() || !userPass.isEmpty()) {
accessWebService();
}
}
});
}
private void accessWebService() {
task = new MyInsertDataTask();
task.execute(new String[]{URL});
}
像魅力一样工作。谢谢大家的时间!!!