Java 使用Android异步Http处理成功请求返回
我是android新手,来自iOS,我对Java及其所有功能不太了解。我正在尝试构建一个用户需要在启动时登录的应用程序。我使用的是一个私有API,使用方式如下:Java 使用Android异步Http处理成功请求返回,java,android,android-asynctask,android-async-http,Java,Android,Android Asynctask,Android Async Http,我是android新手,来自iOS,我对Java及其所有功能不太了解。我正在尝试构建一个用户需要在启动时登录的应用程序。我使用的是一个私有API,使用方式如下: https://apiUrl.com/login?login=login&password=password 它返回给我一个JSon对象: { token: "qqdpo9i7qo3m8lldksin6cq714" } <category android:name="android.intent.cate
https://apiUrl.com/login?login=login&password=password
它返回给我一个JSon对象:
{
token: "qqdpo9i7qo3m8lldksin6cq714"
}
<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>
因此,我在代码中所做的很简单:
<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>
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);
}
<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>
我的实现工作正常,我有一条ToastMessage,它显示在屏幕上,带有“Login Success”(当然,当它失败时是“Login Error”)
<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>
但我不知道如何处理这种成功,以便传递给我创建的其他活动
<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>
我想这样做:
if (apiQuery.loginQuery(...))
show(activityLogged); // Where activityLogged is another activity
<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>
更新
<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>
我添加了以下几行:
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>
我的清单文件如下所示:
<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>
<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回调中的下一个活动
<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>
@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);
}
}
在上述代码中
<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>
Intent i = new Intent(context,LoggedActivity.class);
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>
@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);
}
}
然后从下一个活动(在onCreate()中)检索令牌,如下所示:
<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>
你应该读到:我必须修改一点,它没有编译,
Intent I=newintent(_mainContext,MainActivityLogged.class)_主上下文。起始触觉(i)代码>。但是,当我点击Log按钮时,应用程序退出。您是否已将意向过滤器添加到新活动中?没有必要这样放,或者你能给我整个清单文件吗,好的,我添加了I.addFlags(Intent.FLAG\u ACTIVITY\u NEW\u TASK)代码>现在可以工作了!感谢您的帮助:)我已经使用异步http编写了一个示例,如果需要,您可以查看该示例