2016-12-05 43 views
-1

在我的android应用程序中,我有一个活动和一个Java类,用于执行与数据库的连接,然后从JSON读取响应。阅读积极的回应后,我需要开展另一项活动。使Android活动等待一个类

有办法等到第二课结束?

我试图让类扩展AsyncTask并设置变量,我将返回到onPostExecute方法中的活动,但它不起作用。

用户登陆(=活动)

public void OnLoginUtente(View view) { 
    final String mailU = etMailU.getText().toString(); 
    final String pwU = etPasswordU.getText().toString(); 
    final String typeU = "Login Utente"; 

    DBConnection connection = new DBConnection(typeU, mailU, pwU, this); 
    connection.doLogin(); 
    if(connection2.isSuccess()){ 
     //new actvity.... 
    } 
} 

DBConnection的(=班)

public class DBConnection{ 
private String type, email, password, URL; 
private Context context; 
private boolean success; 

public DBConnection(String t, String e, String p, Context c) { 
    type = t; 
    email = e; 
    password = p; 
    context = c; 
    if (type.equals("Login Utente")) 
     //URL setting 
    else if (type.equals("Login Pizzeria")) 
     //URL setting 
} 

public void doLogin(){ 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         //read the response from the server 
         JSONObject jsonObject = new JSONObject(response); 
         Toast.makeText(context, jsonObject.getString("Status"), Toast.LENGTH_SHORT).show(); 
         if (jsonObject.getString("Esito").equals("true")) { 
          success = true; 
         } else { 
          success = false; 
         } 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
       } 
      }) { 
     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 

      params.put("type", type); 
      params.put("email", email); 
      params.put("password", password); 

      return params; 
     } 

    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(context); 
    requestQueue.add(stringRequest); 
} 

public boolean isSuccess() { 
    return success; 
} 

}

+0

*使Android活动等待一个类* ...可怕的想法...它会阻止UI /主线程 – Selvin

+0

不是,我假设他意味着ActivityA执行异步任务,同时显示取消按钮和进度指示器。任务完成后,启动ActivityB。一个非常普通的模式。然而,Jin请提供更多信息,这似乎是一种常见的情况,并不清楚什么是“不起作用”。 –

+0

@Selvin否则,我必须编写完全相同的代码,以便在另外两个活动中读取来自服务器的响应。我认为一个班可以为我做。 – Facosenpai

回答

0

简而言之第二活动的意图在onResponse回调

if (jsonObject.getString("Esito").equals("true")) { 
    // intent to the second Activity 
    Intent intent = new Intent(context, SecondActivity.class); 
    context.startActivity(intent); 
} 
+0

谢谢你,它的工作原理! – Facosenpai

0

可以按如下修改onResponse方法:

public void onResponse(String response) { 
         try { 
          //read the response from the server 
          JSONObject jsonObject = new JSONObject(response); 
          Toast.makeText(context, jsonObject.getString("Status"), Toast.LENGTH_SHORT).show(); 
          if (jsonObject.getString("Esito").equals("true")) { 
           success = true; 
           Intent intent = new Intent(context, NewActivity.class); 
           startActivity(intent); 
          } else { 
           success = false; 
          } 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 


        } 

替换NewActivity与要启动的活动的名称。