tus / tus-android-client

The tus client for Android.
https://tus.io/
161 stars 46 forks source link

NullPointerException on connection.getHeaderField("Location") #6

Closed hanjun-lin closed 8 years ago

hanjun-lin commented 8 years ago

Hi all,

I am trying the sample code of tus-android-client. I porting it to a HelloWorld sample project which copy all materials (layout and codes) from GitHub. My server URL is set at onCreate method as below:

protected void onCreate(Bundle savedInstanceState) {
...
    client.setUploadCreationURL(new URL("http://211.78.245.94:1080/"));
...
}

When I running on my two Android smartphones (4.4.2 and 6.0.1), both of them get the error message as below:

04-11 17:57:48.130 22375-22375/com.example.helloworld W/System.err: java.lang.NullPointerException 04-11 17:57:48.130 22375-22375/com.example.helloworld W/System.err: at io.tus.java.client.TusClient.createUpload(TusClient.java:117)

public TusUploader createUpload(TusUpload upload) {
...
    String urlStr = connection.getHeaderField("Location");  // Line 117
    if(urlStr.length() == 0) {
        throw new ProtocolException("missing upload URL in response for creating upload");
    }
    URL uploadURL = new URL(urlStr);
...
}

It seems like the connection object cannot get Location this header field.

I solved this problem by mark codes as above and use codes as below: URL uploadURL = connection.getURL();

And get another error message:

04-11 17:51:46.038 14065-14065/com.example.helloworld W/System.err: java.lang.NullPointerException 04-11 17:51:46.038 14065-14065/com.example.helloworld W/System.err: at io.tus.java.client.TusClient.resumeUpload(TusClient.java:172)

public TusUploader resumeUpload(TusUpload upload) {
...
    String offsetStr = connection.getHeaderField("Upload-Offset");  // Line 172
    if(offsetStr.length() == 0) {
        throw new ProtocolException("missing upload offset in response for resuming upload");
    }
...
}

It seems like not the Android OS version problem because two different OS version & device get the same error. It may be the problem is this standard sample code or the server problem.

My server is operating using the tusd which written by Go (the official binary distribution from official web site) and the firewall is opened the port 1080.

Anyone have ideas about solving these two problems?

Acconut commented 8 years ago

First of all, thank you for this detailed report. I will try to reproduce your issues and then come back. However, I noticed that your upload creation URL is missing the path; it probably should be:

client.setUploadCreationURL(new URL("http://211.78.245.94:1080/files/"));
Acconut commented 8 years ago

I was able to fully reproduce your problem and furthermore even able to fix it. Please use the correct URL as in my comment above and it should work. However, since you used a wrong upload creation URL which sent unexpected responses to the tus client, the library had a NullPointerException which was patched in https://github.com/tus/tus-java-client/commit/38a2c9c547036cd54889eed19146e47d5a8ee082.

So please do following:

hanjun-lin commented 8 years ago

Hi Acconut,

I successful upload file to the server after the modification of upload creation URL of your suggestion.

I will suggest that adding comment (explanation, URL format sample) in the sample code and in the document.

For example (my suggestion):

// the format of upload creation URL is http://YourDomainOrIPAddress:Port/files/
// the default port of tusd is 1080 and you must put /files/ in the end of URL
client.setUploadCreationURL(new URL("http://master.tus.io:8080/files/"));

I am trying the resume function in the next step. There is no "pause" button and "resume" button in the sample code. I am trying to make them to try. I will also suggest to add these button in the sample code for developer to try the Resumability of this protocol without re-start the activity of App.

Acconut commented 8 years ago

Great to hear to you were able to solve the problem. I will try and integrate your feedback.

Acconut commented 8 years ago

@hanjun-lin: Just wanted to let you know that the example now features the pause and resume button (see c708cf2201db141a79ef84e8644a6199d1bbb731).

hanjun-lin commented 8 years ago

Thanks a lot. Latest commits of client sides are work now.