[android-developers] Re: HttpURLConnection redirect - doInBackground

2012-07-18 Thread Jacob Hansen
This was caused by the fact that I sent the same headers and did not change 
the "host" header of the request which caused Apache to be confused it 
seems.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] HttpURLConnection redirect - doInBackground

2012-07-18 Thread Jacob Hansen


I'm using HttpURLConnection to do communication with a backend server and 
im doing so in an async task in the doInBackground method as you should.

Now I need to be able to follow 302 redirects, but I'm having some problems 
with this. The issue is that the new location usually will be on another 
host, however when doing the redirect request it seem not to change the URL 
to a new host hence I get a 404 error saying the specified path does not 
exits.

Now I know I could set HtppURLConnection.setFollowRedirect but I need to 
have more control over the redirects so they should not just be followed 
blindly. The Redirect behavour should be controlled by the object who 
called the asynctask (when an asynctask object is created you pass the 
object who creates it in a parameter called _callback).

Heres's my current code:

protected HttpResponse doInBackground(String... req) {

HttpURLConnection urlConnection = null;

try {

urlConnection = (HttpURLConnection) this._url.openConnection();

urlConnection.setConnectTimeout( (int) this._timeout*1000);

String body = req[0];

// set headers / write information to output stream if request is post

 // create the response object

HttpResponse responseObject = null;

try

{

// get status, contenttype, charset...

InputStream in = null;

if (urlConnection.getResponseCode() != -1 && 
urlConnection.getResponseCode() < 300)

{

in = new BufferedInputStream(urlConnection.getInputStream(), 8192);

}

else 

{

in = new BufferedInputStream(urlConnection.getErrorStream(), 8192);

}

responseObject = new HttpResponse(in, status, contentType, charset);

// if redirect

if (status == 302 && this._callback.onRedirect(responseObject) == true)

{

// recall

String url = urlConnection.getHeaderField("location");

Log.v("Async Task", "Redirect location: " + url);

this._url = null;

this._url = new URL(url);

urlConnection.disconnect();

urlConnection = null;

responseObject = this.doInBackground(req);

}

 } catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

} 

// return the response

return responseObject;

} 

// catch some other exceptions

finally 

{

if (urlConnection != null)

{

urlConnection.disconnect();

} }

}

And as said the problem is that the redirect request seem to change the 
path of the URL but not the host. The URL object itself seem to contain the 
right information so I have no idea why this is happening. (I'm getting 
HTML as response which is an 404 error page that includes the server name 
of the old server)

Thanks for any help!

Note: HttpResponse is just an object I created for holding the relevant 
information about the response.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] DOA Object serving a Resource from Android Library Project

2012-06-21 Thread Jacob Hansen
Hi,

I'm currently making a SDK for a payment gateway, however I'm having some 
problems.

The project setup is supposed to be:
*Library Project*

DAO Classe
SDK
Server connectivity class

*Application*

UI

Using the above Library project

 
And from the library project I want to send some images (drawables) for 
example images for what credit cards are available for the payment. These 
credit cards can change depending on different settings so they need to be 
sent dynamically to the application using the SDK.

I'm currently importing the library project by selecting the library 
project under properties in Eclipse (I need to do it with a Jar or 
something to make it closed source but one problem at a time).

Now the problem I get is that I get a NullPointerException when I try to 
set the drawable from the SDK to an ImageView in the app. Here's some code:
DAO Class from the SDK (this class extends Application):
  

> case (MASTERCARD):
> this._logo = getResources().getDrawable(R.drawable.mastercard_securecode);
> break;


This is in the constructor. And then there's a simple getter method to 
access the _logo varible, which is a Drawable. The R.drawable i refer to is 
a ressource located in the Library Projects res folder.

In the application I then want to be able to do something like:

> ImageView img = (ImageView) findViewById(R.id.imageView); 

img.setImageDrawable( DAOObjet.getLogo() );

 

So that whoever implements the SDK is able to decide how the UI should be 
implemented. 

However doing it this way I'm as said getting a NullPointerException on the 
line that serves the Drawable from the SDK (eg the line quoted above in the 
switch/case statement).

I have a feeling I'm loading the ressource in a completely wrong way, but I 
dont know.

Could someone assist me with this? That would be much appreciated!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en