Java 我能';Android中的t persist登录语句
我使用Java 我能';Android中的t persist登录语句,java,android,httpurlconnection,Java,Android,Httpurlconnection,我使用CookiePolicy设置Cookiemanager @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_intro); CookieMana
CookiePolicy
设置Cookiemanager
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_intro);
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
setViews();
}
public void onClick(View v) {
GetTimetable getTimetable = new GetTimetable();
getTimetable.execute();
}
public class GetTimetable extends AsyncTask<Void, Void, Void> {
@Override
protected String doInBackground(Void... params) {
Timetable timetable = new Timetable();
timetable.start();
return null;
}
}
我在Eclipse中仅用Java测试了start()
方法,结果它成功了
通过POST
request登录,通过get
request获取内容。但是,在Android中,似乎并没有持久化登录语句,因为POST请求成功返回了头,但Get请求根本不起作用
我被绊倒了
我真的想知道如何持久化登录语句
谢谢 登录时可以返回令牌吗?@FabioVentureiPastor在Eclipse中,我可以用200状态码获得它。但是,在安卓系统中,我不能。它只返回500个错误。我使用相同的代码。在Android.500中测试时,只需添加cookiePolicy就意味着网站服务器出现了问题,返回的消息是什么?在日志消息中,我在com.Android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)处获得了java.io.FileNotFoundException我不确定问题是否与权限、超时或其他问题有关。登录时是否可以返回令牌?@FabioVentureiPastor在Eclipse中,我可以使用200状态码获得它。但是,在安卓系统中,我不能。它只返回500个错误。我使用相同的代码。在Android.500中测试时,只需添加cookiePolicy就意味着网站服务器出现了问题,返回的消息是什么?在日志消息中,我在com.Android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)处获得了java.io.FileNotFoundException我不确定问题是否与权限、超时或其他有关
public void start() {
try {
URL url = new URL(LOGIN_URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
String urlParameters = URL_PARAMETERS;
DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.writeBytes(urlParameters);
out.flush();
out.close();
} finally {
urlConnection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
URL url = new URL(TARGET_URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuffer response = new StringBuffer();
while (in.readLine() != null) {
response.append(in.readLine());
}
in.close();
System.out.println(response.toString());
} finally {
urlConnection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}