I have written an app which uses an URLConnection to get a .html file.
Everything works fine over wifi. But over 3g the file is not returned correctly.
When i try to access the website via the browser it works fine.
Anyone has a suggestion?
Update:
Here is my code:
URL downloadUrl;
URLConnection downloadConnection;
InputStream inputStream;
byte[] inputBytes;
String[] output;
private void downloadSource(String pUrl)
{
try
{
downloadUrl = new URL(pUrl);
downloadConnection = downloadUrl.openConnection();
downloadConnection.setConnectTimeout(10000);
downloadConnection.setReadTimeout(10000);
inputStream = downloadConnection.getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
inputBytes = new byte[10000];
int i;
int i1 = 0;
while ((i = inputStream.read(inputBytes)) > 0)
{
result.write(inputBytes, 0, i);
result.flush();
i1 += i;
}
result.flush();
result.close();
output = result.toString().split("\n");
}
catch (Exception e)
{
e.printStackTrace();
}
}
Maybe it's a little bit late for answer but I had the same problem, with Wifi the html downloaded had different spaces than the one downloaded with 3G.
I solved it deleting the User-Agent in the connection:
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","");
I hope it helps someone!
Related
I am kind of new in android development and I am having a weird issue.
The following code is supposed to work:
URL url = new URL("http://127.0.0.1/test.html");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
System.out.println("code: "+code);
The problem is, after I do the connection.connect(); nothing happens, even if I add a textX.setText() after the connect, I am not able to do any action.
Any idea what might be the issue?
This is my whole method, all I am trying to do is get some text from the API, which says "OK" actually, but I am not able to make it work.
public void conn (View view)
{
TextView text2 = (TextView)findViewById(R.id.text2);
text2.setText("connecting...");
String output="";
//All working until here
URL url;
HttpURLConnection urlConnection = null;
try {
output="about to connect";
text2.setText(output);
url = new URL("http://127.0.0.1:4444/localweb/api/api.php");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream in = urlConnection.getInputStream();
text2.append("\nabout to get code");
int code = connection.getResponseCode();
text2.setText(Integer.toString(code));
//urlConnection = (HttpURLConnection) url.openConnection();
//urlConnection.connect();
//output=urlConnection.getResponseMessage();
//text2.setText(output);
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
output=output+current;
}
//text2.setText(output);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
There wasn't something 'wrong' with the http client code, the issue is that you cannot launch the httpurlclient from the parent thread as I was trying to do, it must be executed in the background through an AsyncTask, after moving all the httpurlconnection stuff into an additional async function now I am able to get all the web details I needed.
I'm trying to create an android application which depends on JSON responses. Sometimes it takes a lot of time for the server to respond and ends in a time out exception. Therefore I would like to add a restriction like my webservice call should abort after 20seconds if there is no response. Can you please help me achieving this idea.
Thanks in Advance.
You're not giving much details on the actual implementation that you have.
However, messing with the timeout seems like it may be an emergency fix to an underlying problem that should be fixed.
However, using websockets for transport could be a possible (and probably more elegant) solution. They provide a persistent connection between client and server once created.
Using websockets on Android and IOS
There are several ways to achieve the goal.
We can using HttpURLConnection to do the http request.
public String doPost() {
if (!mIsNetworkAvailable) {
return null;
}
try {
URL url = new URL(mURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
for (String key : mHeadersMap.keySet()) {
conn.setRequestProperty(key, mHeadersMap.get(key));
}
conn.setRequestProperty("User-Agent", "Android");
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setRequestProperty("Content-Type", "application/json");
conn.getOutputStream().write(mContent);
conn.getOutputStream().flush();
int rspCode = conn.getResponseCode();
if (rspCode >= 400) {
return null;
}
byte[] buffer = new byte[8 * 1024];
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
while ((len = bis.read(buffer)) > 0) {
baos.write(buffer, 0, len);
}
baos.flush();
final String result = new String(baos.toByteArray());
baos.close();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
setConnectTimeout :Sets the maximum time in milliseconds to wait while connecting.
setReadTimeout:Sets the maximum time to wait for an input stream read to complete before giving up.
Reference: http://developer.android.com/reference/java/net/URLConnection.html
I'm downloading different medias files from my http server; mp3, jpg/png/ and html.
Everything worked fine when I used the now deprecated HttpClient.
I decided to use the HttpURLConnection.
But I encounter a problem with text files(html).
read() blocks on small html files, maybe waiting for a EOF or I don't know what, during few seconds and exits with the Exception "unexpected end of stream".
My code is:
URL url = new URL(urlString);
postParams = String.format("registration=%s&"....);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true); // It's a POST request which replies sending a file
urlConnection.setChunkedStreamingMode(0);
if (fileName.contains(".htm")) { // Tried this to see...
urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + "UTF-8");
}
OutputStream output = urlConnection.getOutputStream();
output.write(postParams.getBytes("UTF-8"));
is = new BufferedInputStream(urlConnection.getInputStream());
/* Get information from the HttpURLConnection automatically fires the request
* http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests
*/
int status = urlConnection.getResponseCode();
if (status == 200) {
int len, size = 0;
byte[] buf = new byte[128 * 1024];
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
try {
while ((len = is.read(buf, 0, buf.length)) > 0) {
os.write(buf, 0, len);
size += len;
}
os.flush();
} catch (IOException e) {
Log.d(Constants.APP_TAG, "IOException." + e); // HTML files end here after few seconds
} finally {
nbFilesDownloaded++;
is.close();
os.close();
file.setReadable(true, false);
file.setWritable(true, false);
}
}
Any idea to explain why it cannot normally exit from read()??
EDIT: I verified that for the html files which cause this exception, my webserver doesn't include Content-Length in the header. Can it be the cause of the problem?
Try to use okhttp http://square.github.io/okhttp/. I have downloaded the files correctly. I hope to help.
Paul.
I was first using HttpURLConnection with my first test. Now I would like to also support https, but it doesn't work. I've been at it all day and so far nothing. Most of the past problems have been related to certificate issues. Weird thing is in my case it downloads the file, but its either corrupted (if its a simple file), or the zips contents are missing (empty). I will post my code to see if maybe I am doing something wrong.
try{
URL url = new URL(stuffs[0]);//<-actual url I am searching https://...
String fileName = stuffs[1];
String optionalFilePath = stuffs[2] == null ? null : stuffs[2];
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(20000);
connection.connect();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
checkErrorCode(connection.getResponseCode());
return false;
}
InputStream in = new BufferedInputStream(connection.getInputStream());
FileOutputStream out = null;
if(optionalFilePath == null)
out = new FileOutputStream(PATH +"/"+fileName);
else {
File newDir = new File(PATH+optionalFilePath);
newDir.mkdirs();
out = new FileOutputStream(PATH + (optionalFilePath==null?"":optionalFilePath) +"/"+fileName);
}
byte[] buffer = new byte[1024];
int count;
while((count = in.read(buffer)) != -1){
out.write(buffer, 0, count);
}
out.flush();
out.close();
in.close();
}
Upon further debugging, I found out the content length is -1. So I guess it makes sense why the zip is empty. Now I am not too sure why it returns -1. I download it on a web browser correctly. So I know it exists.
To Download file via https You should accept https certificate from application Trusting all certificates using HttpClient over HTTPS
And https file download in android causing exception
For Downlod zip or any check this out
Download a file with Android, and showing the progress in a ProgressDialog
I believe the answer is that you are calling connect().
URL url = new URL(stuffs[0]);//<-actual url I am searching https://...
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(20000);
connection.connect();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
checkErrorCode(connection.getResponseCode());
return false;
}
InputStream in = new BufferedInputStream(connection.getInputStream());
Try not calling connection.connect, and moving the response code check after the line that calls connection.getInputStream().
I make an application , this can download file form server.
use this code >>>
public int startDownload(String url, String filename) {
// create url connector
URL u;
byte[] buffer = new byte[1024];
try {
u = new URL(url + filename);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();
m_lMaxDownloadSz = c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(FILE_PATH, filename));
m_bCancelDownload = false;
m_lCurrentDownloadSz = 0;
int len = 0;
while ((len = in.read(buffer, 0, 1024)) > 0) {
// if download is canceled.
if (m_bCancelDownload) {
f.close();
c.disconnect();
return FILE_DOWNLOAD_CANCELED;
}
if (knot++ >= PROGRESS_STEP) {
knot = 0;
myProgressDialog.setProgress(GetDownloadStatus());
}
f.write(buffer, 0, len);
m_lCurrentDownloadSz += len;
}
f.close();
c.disconnect();
} catch (Exception e) {
return FILE_DOWNLOAD_FAILED;
}
if (GetDownloadStatus() == 100) {
return FILE_DOWNLOAD_FINISHED;
} else {
return FILE_DOWNLOAD_FAILED;
}
}
and I want to use with PHP force download , but it not work , ordinary it use with file path like 'app/aaa.apk' it work! ,and I change to PHP file like 'php/forcedl.php' it not work.
I needs to use with php force download, How do I use?
ps. i have little english language skill , cause english language is not my main language
thank you
I discover for my answer
android-Java Code
__example:
byte[] buffer = new byte[1024];
String url = "http://www.bla-bla.com/forcedownload.php"
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
filesize = execute.getEntity().getContentLength();
fileOutput = new FileOutputStream(new File(FILE_PATH, "file_copyformserver.apk"));
while ((len = content.read(buffer, 0, 1024)) > 0) {
fileOutput.write(buffer, 0, len);
Thread.sleep(100);
}
fileOutput.close();
} catch (Exception e) {
e.printStackTrace();
}
php file
__example:
$file = '/home/bla-bla/domains/bla-bla.com/file/file.apk'; //not public folder
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.android.package-archive');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
this is short code , sorry if cannot run. :)
This is perhaps not the best way to do it, but have you considered renaming the file upon successfully downloading it? I haven't tried it, but I believe you can do it using the File.renameTo() method in Android.
Here's some pseudo code that I think will work, can't try it out right now though:
File.renameTo(new File(FILE_PATH, filename.replace(".apk", ".php")));