Reputation: 84
I'm currently attempting to use cookies (with no success), i can get it's value just fine but sending it back is a whole different story.
I'm using two AsyncTask classes, two buttons and two textboxes. After clicking the first button the first class accesses the URL and acquires the cookie saving it into the first Textbox as a string value (works fine). The second click however is supposed to get the cookie from the textbox and send it back to a second URL to show an online message (does not work).
the code for the two buttons:
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);
}
});
The AsyncTasks :
//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);
}
}
//----------------------
Setting the cookie value in the second link does not work and only returns a different cookie when checking it value again..Should note that using cookies with Android is very poorly documented and most documentations are based on using the deprecated HttpClient instead of the UrlConnection, also i based my technique from this example.
Upvotes: 0
Views: 4583
Reputation: 96
In Second AsyncTask, Use "Cookie" instead of "Set-Cookie" in "setRequestProperty" function.
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);
}
}
The above method is very hard and tough for state management. You can use another simple method as given on this link.
Upvotes: 1