我是Android上的新手和客户端 - 服务器之间的知识
我正在制作一个我需要登录的Android应用程序(就像任何社交网络一样)。我已经在我的数据库中使用MySQL进行了注册活动保存数据。登录时出现问题。我可以验证用户名和密码是否正确但是为了不必再次登录,直到服务器会话关闭,我需要保存服务器上的PHPSESSID并在每次我想看到"受保护的时候检查它#34;我的社交网络的主页或个人资料等活动。
如果服务器上的会话cookie不再存在,我需要再次登录,以便创建新的会话cookie。
我的问题是:
如何从登录脚本中获取PHPSESSID?
将它保存在sharedPreferences中是否正确,以便将其发送到服务器进行检查?
现在这里是我的代码单元:
`public String sendPostRequest(String requestURL,
HashMap postDataParams){
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setUseCaches(true);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
} else {
response = "Error";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}`