Reputation: 41
I created this code for downloading web content and I used Asynctask, but DownloadTask
class doesn't return results. However, when I get a log in this class it shows the content of website. So the DownloadTask
class downloads the content but doesn't return it!
This is my code:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection)url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTask task = new DownloadTask();
String result = null;
try {
result = task.execute("http://www.stackoverflow.com").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.i("Log", "Result ...>>>>>>>" + result);
}
}
Upvotes: 1
Views: 3653
Reputation: 21
//This alternative worked for me.Try the Following code
BufferedReader br = new BufferedReader(reader);
String inputLine;
while ((inputLine = br.readLine()) != null)
result += inputLine;
in.close();
//Instead of
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();}
Upvotes: 2
Reputation: 11
Your code is correct but you have to add permission to connect internet
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.waleed.downloadtask">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 1
Reputation: 594
First make sure that device is connected to internet.If not then logs will show error Unknown Host Exception Since your device is not connected to internet.And it cannot download content from internet.Your device should be connected to internet. Then try the following code `
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTask task = new DownloadTask();
String result = null;
try {
//sending utl to class method doInBackground()
result = task.execute("http://www.ecowebhosting.co.uk").get();
}
catch(Exception e)
{
e.printStackTrace();
}
Log.i("Result",result);
}
//type of vatiable which we will send to class
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result="";
URL url;
HttpURLConnection urlConnection = null;
try
{
url=new URL(urls[0]);
urlConnection=(HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while(data!=-1)
{
char current=(char) data;
result+=current;
data=reader.read();
}
return result;
}
catch(Exception e)
{
e.printStackTrace();
return "Failed";
}
}
}
}
`
Upvotes: 0
Reputation: 13
did you add permission in Manifests to access the Internet?
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
}
catch (Exception e) {
e.printStackTrace();
}
return Null; // This one should return null or something else if you
didnt get a proper url
}
}
Upvotes: 0