使用 Android 异步 Http 处理成功请求 return
Handle success request return with Android Async Http
我是 android 的新手,来自 iOS 我不太了解 Java 及其所有功能。我正在尝试构建一个用户需要在启动时登录的应用程序。我使用的是私人 API ,我喜欢这样使用:
https://apiUrl.com/login?login=login&password=password
它 returns 我 JSon 对象 :
{
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 出现在屏幕上 "Login Success"(当然失败时 "Login Error")
但我不知道如何处理成功以传递给我创建的另一个 activity。
我想做这样的事情:
if (apiQuery.loginQuery(...))
show(activityLogged); // Where activityLogged is another activity
更新
我添加了这些行:
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>
您可以简单地在 onSuccess 回调
中编写一个 Intent 以移动到下一个 activity
@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");
Intent i = new Intent(context,LoggedActivity.class);
context.startActivity(i);
}
}
在上面的代码中
Intent i = new Intent(context,LoggedActivity.class);
startActivity(i);
这用于导航到下一个 page.Also 确保在清单文件中声明 activity。
您还可以通过 Intent 将一些数据传递给下一个 activity,如下所示:
@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");
Intent i = new Intent(context,LoggedActivity.class);
i.putExtra("token", token);
startActivity(i);
}
}
然后从下一个 activity(在 onCreate() 内)检索令牌,如下所示:
String token = getIntent().getStringExtra("token");
我是 android 的新手,来自 iOS 我不太了解 Java 及其所有功能。我正在尝试构建一个用户需要在启动时登录的应用程序。我使用的是私人 API ,我喜欢这样使用:
https://apiUrl.com/login?login=login&password=password
它 returns 我 JSon 对象 :
{
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 出现在屏幕上 "Login Success"(当然失败时 "Login Error")
但我不知道如何处理成功以传递给我创建的另一个 activity。
我想做这样的事情:
if (apiQuery.loginQuery(...))
show(activityLogged); // Where activityLogged is another activity
更新
我添加了这些行:
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>
您可以简单地在 onSuccess 回调
中编写一个 Intent 以移动到下一个 activity @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");
Intent i = new Intent(context,LoggedActivity.class);
context.startActivity(i);
}
}
在上面的代码中
Intent i = new Intent(context,LoggedActivity.class);
startActivity(i);
这用于导航到下一个 page.Also 确保在清单文件中声明 activity。
您还可以通过 Intent 将一些数据传递给下一个 activity,如下所示:
@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");
Intent i = new Intent(context,LoggedActivity.class);
i.putExtra("token", token);
startActivity(i);
}
}
然后从下一个 activity(在 onCreate() 内)检索令牌,如下所示:
String token = getIntent().getStringExtra("token");