在我的Android应用程序用户输入用户名和密码,我在将它们发送到服务器后检查响应。如果结果是肯定的,我正在改变活动并发送不同的请求。但是服务器响应我"你必须登录。"尽管我只创建了一个HttpURLConnection。我该如何解决这个问题。
private static HttpURLConnection conn;
private static String _sessionIdCookie;
public void fetchJSON(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
if (_sessionIdCookie != null)
conn.setRequestProperty("Cookie", _sessionIdCookie);
//I am getting sessionId from respond. Server send my session ID in JSON.
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
String data = convertStreamToString(stream);
readAndParseJSON(data); //This function parses the data.
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}