Android 工作室:使用 cookie
Android studio : using cookies
我目前正在尝试使用 cookie(没有成功),我可以很好地获取它的价值,但将其发回则完全不同。
我正在使用两个 AsyncTask classes、两个按钮和两个文本框。单击第一个按钮后,第一个 class 访问 URL 并获取将其作为字符串值保存到第一个文本框中的 cookie(工作正常)。
然而,第二次点击应该从文本框中获取 cookie 并将其发送回第二个 URL 以显示在线消息(不起作用)。
两个按钮的代码:
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//executing the first AsynTask
new DownloadWebpageTask().execute(FirstLink);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] ar= new String[2];
//save the value of the second link and the cookie value in an array
ar[0]=SecondLink;
ar[1]= String.valueOf(textBox1.getText());
//executing the second AsynTask
new showC().execute(ar);
}
});
异步任务:
//First AsyncTask
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
URL url = null;
URLConnection conn=null;
String output="";
try {
url = new URL(urls[0]);
conn = url.openConnection();
conn.connect();
return conn.getHeaderField("Set-Cookie");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Error";
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
//store the cookie value as string, something like : PHPSESSID=....;
textBox1.setText(result);
}
}
//Second AsyncTask
private class showC extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
URL url = null;
HttpURLConnection conn=null;
String output="";
try {
url = new URL(urls[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Set-Cookie", urls[1]);
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
output=br.readLine();
return output+" "+conn.getHeaderField("Set-Cookie");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Error";
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
text2.setText(result);
}
}
//----------------------
在第二个 link 中设置 cookie 值不起作用,再次检查它的值时只有 returns 一个不同的 cookie..应该注意使用 Android 的 cookie 非常文档很少,大多数文档都是基于使用已弃用的 HttpClient 而不是 UrlConnection,我的技术也是基于此 example.
在第二个 AsyncTask 中,在 "setRequestProperty" 函数中使用 "Cookie" 而不是 "Set-Cookie"。
private class showC extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
URL url = null;
HttpURLConnection conn=null;
String output="";
try {
url = new URL(urls[0]);
conn = (HttpURLConnection) url.openConnection();
// Modification
conn.setRequestProperty("Cookie", urls[1]);
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
output=br.readLine();
return output+" "+conn.getHeaderField("Set-Cookie");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Error";
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
text2.setText(result);
}
}
以上方法对于状态管理来说是非常困难和艰难的。您可以使用此 link.
中给出的另一种简单方法
我目前正在尝试使用 cookie(没有成功),我可以很好地获取它的价值,但将其发回则完全不同。
我正在使用两个 AsyncTask classes、两个按钮和两个文本框。单击第一个按钮后,第一个 class 访问 URL 并获取将其作为字符串值保存到第一个文本框中的 cookie(工作正常)。 然而,第二次点击应该从文本框中获取 cookie 并将其发送回第二个 URL 以显示在线消息(不起作用)。
两个按钮的代码:
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//executing the first AsynTask
new DownloadWebpageTask().execute(FirstLink);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] ar= new String[2];
//save the value of the second link and the cookie value in an array
ar[0]=SecondLink;
ar[1]= String.valueOf(textBox1.getText());
//executing the second AsynTask
new showC().execute(ar);
}
});
异步任务:
//First AsyncTask
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
URL url = null;
URLConnection conn=null;
String output="";
try {
url = new URL(urls[0]);
conn = url.openConnection();
conn.connect();
return conn.getHeaderField("Set-Cookie");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Error";
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
//store the cookie value as string, something like : PHPSESSID=....;
textBox1.setText(result);
}
}
//Second AsyncTask
private class showC extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
URL url = null;
HttpURLConnection conn=null;
String output="";
try {
url = new URL(urls[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Set-Cookie", urls[1]);
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
output=br.readLine();
return output+" "+conn.getHeaderField("Set-Cookie");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Error";
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
text2.setText(result);
}
}
//----------------------
在第二个 link 中设置 cookie 值不起作用,再次检查它的值时只有 returns 一个不同的 cookie..应该注意使用 Android 的 cookie 非常文档很少,大多数文档都是基于使用已弃用的 HttpClient 而不是 UrlConnection,我的技术也是基于此 example.
在第二个 AsyncTask 中,在 "setRequestProperty" 函数中使用 "Cookie" 而不是 "Set-Cookie"。
private class showC extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
URL url = null;
HttpURLConnection conn=null;
String output="";
try {
url = new URL(urls[0]);
conn = (HttpURLConnection) url.openConnection();
// Modification
conn.setRequestProperty("Cookie", urls[1]);
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
output=br.readLine();
return output+" "+conn.getHeaderField("Set-Cookie");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "Error";
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
text2.setText(result);
}
}
以上方法对于状态管理来说是非常困难和艰难的。您可以使用此 link.
中给出的另一种简单方法