gpt4 book ai didi

java - Android - 在 Activity 之间保持用户记录

转载 作者:行者123 更新时间:2023-12-01 11:51:44 25 4
gpt4 key购买 nike

在我的应用程序中,我有第一个 Activity ,允许您登录网络服务。如果服务器允许用户连接,则应调用 TabHost Activity 。在 TabHost Activity 中,我有 3 个不同的 Activity :

  • HomeActivity:它仅显示一个网页 View
  • HistoryActivity:它应该显示一个 ListView,我在其中插入通知历史记录
  • SettingsActivity:显示应用的一些设置

在 HistoryActivity 中,我必须调用 Web 服务来下载通知历史记录列表。要调用此服务,我必须保持用户登录,我该怎么做?

我使用以下代码连接到历史记录服务:

public void postData(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

try {
StringEntity jsonSend = new StringEntity("{\"history\": true}");
httpPost.setHeader("Content-type", "application/json; charset=UTF-8");
httpPost.setEntity(jsonSend);
HttpResponse response = httpClient.execute(httpPost);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();

for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
String json = builder.toString();
Log.d("HISTORY", json);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

我用它来登录,效果很好。要连接到登录服务,我使用以下代码:

public void postData(final String username, final String password) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("pollingId", "XXXXXXXXX"));

httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpClient.execute(httpPost);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();

for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}

String json = builder.toString();

JSONObject jsonObject = new JSONObject(json);
Boolean success = jsonObject.getBoolean("success");

if (success == true) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "ACCESSO EFFETTUATO!", Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this, TabBarActivity.class);
startActivity(i);
}
});
}

} catch (ClientProtocolException e) {
Log.d("CLIENT EXCEPTION", "ERRORE: " + e);
}catch (IOException e) {
Log.d("I/O EXCEPTION", "ERRORE: " + e);
} catch (JSONException e) {
e.printStackTrace();
}
}

如果我用它来获取通知历史记录,JSON 会告诉我用户未登录。我该如何修复它?谢谢

更新我尝试遵循您的建议,但得到了相同的结果。修改后的代码如下:

MainActivity.java

public void postData(final String username, final String password) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://extranet.gruppotesta.it/srv/at-brain/login.jsp");

try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("pollingId", "XXXXXXX"));

httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpClient.execute(httpPost);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();

for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}

String json = builder.toString();

JSONObject jsonObject = new JSONObject(json);
Boolean success = jsonObject.getBoolean("success");

if (success == true) {
Header[] cookie = response.getHeaders("cookie");
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("cookie", cookie.toString());
editor.commit();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "ACCESSO EFFETTUATO!", Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this, TabBarActivity.class);
startActivity(i);
}
});
}

} catch (ClientProtocolException e) {
Log.d("CLIENT EXCEPTION", "ERRORE: " + e);
}catch (IOException e) {
Log.d("I/O EXCEPTION", "ERRORE: " + e);
} catch (JSONException e) {
e.printStackTrace();
}
}
}

HistoryActivity.java

   public void postData(String url) {
HttpClient httpClient = new DefaultHttpClient();

SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
String stringCookie = sharedPreferences.getString("cookie", null);
BasicCookieStore cookieStore = new BasicCookieStore();
Cookie cookie = new BasicClientCookie("login", stringCookie);
cookieStore.addCookie(cookie);

HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpPost httpPost = new HttpPost(url);

try {
StringEntity jsonRequest = new StringEntity("{\"history\": true}");
httpPost.setEntity(jsonRequest);
httpPost.setHeader("Content-type", "application/json; charset=UTF-8");
httpPost.setHeader("Cookie", cookie.toString());

HttpResponse response = httpClient.execute(httpPost, localContext);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();

for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
String json = builder.toString();
Log.d("HISTORY", json);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

当我尝试运行应用程序时,服务器回答我错误“用户未登录”。我的代码有什么问题?

最佳答案

当您第一次登录时,网络服务必须为您提供某种访问 token - 也许是一个有效的用户 ID。您需要在 SharedPreference 中存储和检索此用户 ID 。此用户 ID 必须作为参数传递给所有后续 Web 服务,以指示用户确实已登录。

有关如何获取和设置 SharedPreference 的官方教程是 here .

关于java - Android - 在 Activity 之间保持用户记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28763326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com