我是新來的android,來自iOS我對Java及其所有功能瞭解不多。我正在嘗試構建一個應用程序,用戶需要在啓動時進行登錄。我用我使用一個專用的API,如: https://apiUrl.com/login?login=login&password=password
它返回我JSON對象:使用Android異步處理成功請求返回Http
{
token: "qqdpo9i7qo3m8lldksin6cq714"
}
所以我在做什麼在我的代碼很簡單:
MainActivity.java:
Button button = (Button) findViewById(R.id.loginButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String login = (String) ((EditText) findViewById (R.id.userName)).getText().toString();
String password = (String) ((EditText) findViewById (R.id.password)).getText().toString();
if (login != "" && password != "")
{
HashMap<String, String> postElements = new HashMap<String, String>();
postElements.put("login", login);
try {
postElements.put("password", URLEncoder.encode(password, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Button button = (Button) findViewById(R.id.loginButton);
button.setText("Login in ...");
String queryLogin = "https://apiUrl.com/login?";
String urlString = "";
try {
urlString = "login=";
urlString += URLEncoder.encode(login, "UTF-8");
urlString += "&password=";
urlString += URLEncoder.encode(password, "UTF-8");
} catch (UnsupportedEncodingException e) {
// if this fails for some reason, let the user know why
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
apiQuery.loginQuery(queryLogin, urlString);
}
apiQuery是類型APIQuery的:
public void loginQuery(String url, String urlString) {
// Prepare your search string to be put in a URL
// It might have reserved characters or something
// Create a client to perform networking
AsyncHttpClient client = new AsyncHttpClient();
// Have the client get a JSONArray of data
// and define how to respond
client.get(url + urlString,
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject jsonObject) {
String token = "";
if (jsonObject.has("token")) {
/*Toast.makeText(_mainContext, "Login Success!", Toast.LENGTH_LONG).show();*/
token = jsonObject.optString("token");
// 8. For now, just log results
Log.d("APIQuery Success", jsonObject.toString());
}
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Display a "Toast" message
// to announce the failure
Toast.makeText(_mainContext, "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
// Log error message
// to help solve any problems
Log.e("APIQuery Failure", statusCode + " " + throwable.getMessage());
}
});
}
我的實現工作正常,我有一個ToastMessage屏幕上顯示「登錄成功」(或「登錄錯誤」當它失敗當然)
但我不知道如何處理該成功,以便傳遞給我創建的其他活動。
我願做這樣的事情:
if (apiQuery.loginQuery(...))
show(activityLogged); // Where activityLogged is another activity
UPDATE
我添加這些行:
if (jsonObject.has("token"))
{
/*Toast.makeText(_mainContext, "Login Success!", Toast.LENGTH_LONG).show();*/
token = jsonObject.optString("token");
// 8. For now, just log results
Log.d("APIQuery Success", jsonObject.toString());
Intent i = new Intent(_mainContext, MainActivityLogged.class);
_mainContext.startActivity(i);
}
我的清單文件看起來像:
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- ATTENTION: This data URL was auto-generated. We recommend that you use the HTTP scheme.
TODO: Change the host or pathPrefix as necessary. -->
<data
android:host="epidroid.charvoz.example.com"
android:pathPrefix="/mainactivitylogged"
android:scheme="http" />
</intent-filters>
你應該閱讀:http://developer.android.com/training/basics/firstapp/starting-activity.html – pedromss