我无法重定向到基于来自 Web 服务器的请求的活动



我有一个凌空抽射,它将手机的Mac地址发布到服务器,并以字符串"yes"返回响应,天气后端中的条件满足,当我检查存储的变量时,响应是否否则其他条件仅工作天气条件为真或假。这一切都在初始屏幕中。

这是一个包含凌空抽射的异步任务。全局声明的 MacAuth 变量。

private class BackgroundTask extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object[] params) {

/*  Use this method to load background
* data that your app needs. */
try {
Thread.sleep(3000);
final String mac = "{" + ""address"" + ":" + """ + MAC + """ + "}";
String URL = "http://.....";
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject objRes = new JSONObject(response);
MacAuth = objRes.getString("statusCode");
// Toast.makeText(getApplicationContext(), objRes.toString(), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
//Log.e("TAG", "Error " + error.getMessage());
e.getStackTrace();
// Toast.makeText(getApplicationContext(), mac, Toast.LENGTH_LONG).show();
}
Log.i("macadresssented",mac);
Log.i("MAC", response);
Log.i("MacAuth",MacAuth);
//  Toast.makeText(getApplicationContext(), MacAuth, Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//   Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
Log.e("TAG", "Error " + error.getMessage());
Log.v("VOLLEY", error.toString());
error.printStackTrace();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mac == null ? null : mac.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
Log.v("Unsupported Encoding ", mac);
return null;
}
}
};
requestQueue.add(stringRequest);
if(MacAuth.equals("yes")) {Log.i("checkifno", MacAuth);
intent = new Intent(Splash_Screen.this, MainActivity.class);
startActivity(intent);
finish();

} else {Log.i("checkifyes", MacAuth);
intent = new Intent(Splash_Screen.this, Error_Mac_Authentication.class);
startActivity(intent);
finish();
}
return params;
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute (Object o){
super.onPostExecute(o);
}
}
}

这是日志猫输出

I/macadresssented: {"address":"36:4E:07:GD:4B:90"}

I/MAC: {"statusCode":"yes"}

I/MacAuth:是

try {
JSONObject objRes = new JSONObject(response);
MacAuth = objRes.getString("statusCode");
// Toast.makeText(getApplicationContext(), objRes.toString(), 
Toast.LENGTH_LONG).show();
if(MacAuth.equals("yes")) {Log.i("checkifno", MacAuth);
intent = new Intent(Splash_Screen.this, MainActivity.class);
startActivity(intent);
finish();
} else {Log.i("checkifyes", MacAuth);
intent = new Intent(Splash_Screen.this, Error_Mac_Authentication.class);
startActivity(intent);
finish();
}
} catch (JSONException e) {
//Log.e("TAG", "Error " + error.getMessage());
e.getStackTrace();
// Toast.makeText(getApplicationContext(), mac, Toast.LENGTH_LONG).show();
}

您可以在 try-catch 语句中尝试。

StringRequest request = new StringRequest(Request.Method.POST, UrlsAvision.URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String MacAuth =null ;
Intent intent;
try {
JSONObject objRes = new JSONObject(response);
MacAuth = objRes.getString("statusCode");
// Toast.makeText(getApplicationContext(), objRes.toString(), Toast.LENGTH_LONG).show();
if(MacAuth.equals("yes")) {Log.i("checkifno", MacAuth);
intent = new Intent(Splash_Screen.this, MainActivity.class);
startActivity(intent);
finish();

} else {Log.i("checkifyes", MacAuth);
intent = new Intent(Splash_Screen.this, Error_Mac_Authentication.class);
startActivity(intent);
finish();
}
} catch (JSONException e) {
//Log.e("TAG", "Error " + error.getMessage());
e.getStackTrace();
// Toast.makeText(getApplicationContext(), mac, Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new Hashtable<>();
params.put("yourParams", "");
return params;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppWebService.getInstance(context).addToRequestQueue(request);

我把其他东西移到了onResponse。现在它工作正常。

public void onResponse(String response) {
try {
JSONObject objRes = new JSONObject(response);
MacAuth = objRes.getString("statusCode");
// Toast.makeText(getApplicationContext(), objRes.toString(), Toast.LENGTH_LONG).show();
if(MacAuth.equals("yes")) {
Log.i("checkMain", MacAuth);
intent = new Intent(Splash_Screen.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Log.i("checkErr", MacAuth);
intent = new Intent(Splash_Screen.this, Error_Mac_Authentication.class);
startActivity(intent);
finish();
}
} catch (JSONException e) {
//Log.e("TAG", "Error " + error.getMessage());
e.getStackTrace();
// Toast.makeText(getApplicationContext(), mac, Toast.LENGTH_LONG).show();
}
Log.i("macadresssented",mac);
Log.i("MAC", response);
Log.i("MacAuth",MacAuth);
//  Toast.makeText(getApplicationContext(), MacAuth, Toast.LENGTH_LONG).show();
}

最新更新