简体   繁体   中英

Android - Data not downloading using mobile data/3G

I have an android app that the user can update by pressing an "UPDATE" button. This works perfect on wifi . However, on mobile data , it has a problem. The UPDATE process connects to my REST server and actually connects to 3 URLs for different data. On mobile data, the first and last URLs work fine, but the second one returns blank data. Like I said, on WIFI, all 3 URLs work perfect, but on 3G, they do not. It is always only the 2nd URL that is causing issues.

Here is my code. It is pretty standard:

//FIRST
URL url = new URL(Constants.firstURL); 
String line = null; 
InputStream is = (InputStream) url.getContent(); 
StringBuilder sb = new StringBuilder(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
while((line = reader.readLine()) != null)
{ 
    sb.append(line); 
} 
firstRawData = sb.toString().replace(" ", "").replace("\n", ""); 
is.close(); 

//SECOND
url = new URL(Constants.secondURL); 
line = null; 
is = (InputStream) url.getContent(); 
sb = new StringBuilder(); 
reader = new BufferedReader(new InputStreamReader(is)); 
while((line = reader.readLine()) != null)
{ 
    sb.append(line); 
} 
secondRawData = sb.toString().replace(" ", "").replace("\n", ""); 
is.close();

//THIRD
url = new URL(Constants.thirdURL); 
line = null; 
is = (InputStream) url.getContent(); 
sb = new StringBuilder(); 
reader = new BufferedReader(new InputStreamReader(is)); 
while((line = reader.readLine()) != null)
{ 
    sb.append(line); 
} 
thirdRawData = sb.toString().replace(" ", "").replace("\n", ""); 
is.close();

I would appreciate any help with this as I have never encountered this issue before.

Thank you.

EDIT: There are no errors anywhere that I can see and I have the URL stuff in a try/catch which catch MalformedURLException , IOException , NullPointerException and no exceptions are thrown. Logcat doesnt tell me anything either.

OK so I answered this myself after a while but forgot to reply.

The problem was the fact that I was calling this method from within a thread. The thread was going off to work, while the rest of the code was moving forward. By the time it moved forward, the 2nd URL just simply hadn't finished.

When I moved the call outside the thread and made it run sequentially, then everything was fine!

I was thinking I had to run any internet retrieval stuff in a thread but I think these calls are already in threads so it wasnt needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM