2016-12-05 44 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與要啓動的活動的名稱。