Reputation: 1
I'm writing an application that has several features which use HTTPConnection to connect to the internet and download data. I have tested on several devices, but the connection inside the app seems to be hit or miss on different devices. I be;lieve that the connection did not work on Samsung devices. I'm thinking that because it is not yet an "official" licensed application, that the connection is failing. Could it be some sort of OS difference?
The code below is supposed to connect to the internet and download words to a file. It works on some devices, but on others it ends up creating a blank file with and throwing an exception.
try{
URL url = new URL("*" + currQuickList + ".txt");
URLConnection urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
int i = 0;
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String str;
int count = Integer.parseInt(in.readLine());
int[] a = createOrderedList(count,quickListLen);
int d = 0;
int g = 0;
while ((str = in.readLine()) != null && d < a.length) {
// str is one line of text; readLine() strips the newline
// character(s)
if(a[d] == g)
{words.add(str);
d++;
}
g++;
}
in.close();
OutputStream fo = new FileOutputStream(dir);
PrintWriter p = new PrintWriter(fo);
p.print(packageWords2(words) +"\n");
p.close();
fo.close();
System.out.println("file created: "+dir);
//files.add(wordText);
}
catch(Exception e){
e.printStackTrace();
errorDialog(e);
}
Upvotes: 0
Views: 102
Reputation: 562
Check your internet connection before download or using internet .You can try this
public static boolean isConnectingToInternet(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
Upvotes: 2