Download website content one by one through Async task - java

I have an application in which I am trying to download an ArrayList of urls using an Async Task Manager and and than show the content of per website one by one.I am so confused please help me.I have also tried to use a for loop on the execute method but it thorws me an error.
Please let me know what i have to do with the required code.
Thanks.
Async task
public class DownloadWeb extends AsyncTask<String,Void,String>{
#Override
protected String doInBackground(String... urls) {
String result = "";
HttpURLConnection connection;
URL myUrl;
try{
myUrl = new URL(urls[0]);
connection = (HttpURLConnection) myUrl.openConnection();
//!!!!!!!!! The page will not re direct!!!!!!!!!!!!!!!!//
String redirect = connection.getHeaderField("Location");
if(redirect != null){
connection = (HttpURLConnection) new URL(redirect).openConnection();
}
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while((line=reader.readLine()) != null){
stringBuilder.append(line);
result = stringBuilder.toString();
}
return result;
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
}
The onCreate method
DownloadWeb task = new DownloadWeb();
try {
String res = task.execute(arr.get(0)).get();
}
catch(Exception e){
e.printStackTrace();
}

try to execute your async task on Executor
asyncTask1.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
// asyncTask2 will only be done AFTER asyncTask1 is done
asyncTask2.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
or you can create a custom queue and execute add your asyncTask in there
refer https://developer.android.com/reference/android/os/AsyncTask.html#SERIAL_EXECUTOR

Related

Android app, HttpURLConnection GET method, AsyncTask getting tag instead of result

It is simple app that needs to send two numbers using GET and reads back.
Button btnSaberi;
EditText txtAunos, txtBunos;
TextView txtIspis;
String sturl = "http://xxx.xxx.xx.x/PhpProject1/index.php";
Integer a, b;
HttpURLConnection httpURLConnection;
URL url;
public void posalji(View view){
a = Integer.parseInt(txtAunos.getText().toString());
b = Integer.parseInt(txtBunos.getText().toString());
sturl = sturl+"?a=" + a + "&b=" + b;
new Posalji().execute(sturl);
}
class Posalji extends AsyncTask<String, String, String> {
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
txtIspis.setText("Rezultat je : " + s);
}
#Override
protected String doInBackground(String... strings) {
try {
url = new URL(strings[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
rez = br.readLine();
}
return rez;
}
}
}
Using Emulator.
I have changed and now instead of showing me php output it shows me html tag.
"<!DOCTYPE html>"
Please remove txtIspis.setText("Rezultat je : " + br.readLine()); line from AsyncTask doInBackground. In doInBackground we never update UI like this. you can use onPostExecute for update your textbox values.
Please read this doc https://developer.android.com/reference/android/os/AsyncTask
As you are calling br.readLine(); just once, it is reading only the first line of data from the BufferedReader.
To read all the lines of data from BufferedReader you need to use a loop.
Update your doInBackground method
#Override
protected String doInBackground(String... params){
String url = params[0];
String result;
try {
URL myUrl = new URL(url);
HttpURLConnection connection =(HttpURLConnection) myUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect()
InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
String inputLine;
while((inputLine = reader.readLine()) != null){
stringBuilder.append(inputLine);
}
reader.close();
streamReader.close();
result = stringBuilder.toString();
}
return result;
}

Make simply HTTP request and receive the content

I have found a working code for makeing simply HTTP requests here, from How can I make a simple HTTP request in MainActivity.java? (Android Studio) and I am going to post it below (with some changes, if I am not wrong it is now necessery to use try{} catch{}). But I would like to ask how I can receive the content? I work with the code in the following way:
GetUrlContentTask req = new GetUrlContentTask();
req.execute("http://192.168.1.10/?pin=OFF1");
textView3.setText(req.doInBackground("http://192.168.1.10/?pin=OFF1"));
GetUrlContentTask
private class GetUrlContentTask extends AsyncTask<String, Integer, String> {
protected String doInBackground(String... urls) {
// String content1 = "";
try {
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String content = "", line;
while ((line = rd.readLine()) != null) {
content += line + "\n";
}
// content1 = content;
}
catch (Exception e) {
e.printStackTrace();
}
// return content1; - returns "", wrong
return "aaa";
//does not work return content;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(String result) {
// this is executed on the main thread after the process is over
// update your UI here
}
}
Add this to your onPostExecute(String result) call
protected void onPostExecute(String result) {
// this is executed on the main thread after the process is over
// update your UI here
setMyData(result);
}
And then fill your textfield or whatever other data you need to update.
private void setMyData(String result){
textView3.setText(result);
}
Where exactly are you stuck? Your code mostly correct although you may need to rearrange it slightly. Your scope is a bit off and your commented code almost gets there. See below
protected String doInBackground(String... urls) {
String content = "", line = "";
HttpURLConnection httpURLConnection;
BufferedReader bufferedReader;
URL url;
try {
for(String uri : urls) {
url = new URL(uri);
url = new URI(url.toURI().getScheme(), url.getAuthority(), url.getPath(), "pin=" + URLEncoder.encode("&OFF1", "UTF-8"), null).toURL();
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while ((line = bufferedReader.readLine()) != null) {
content += line + System.lineSeparator();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch(Exception e) {
e.printStackTrace();
}
}
return content;
}
That will return a String representation of your page. If the page contains HTML it will return text/html, if it contains text it will return just text.
Then as a previous user stated you can set the response on your GUI
protected void onPostExecute(String result) {
textView3.setText(result);
}
The above example will technically work. However, I would highly recommend using http://loopj.com/android-async-http/ over HttpURLConnection. Your code will be much nicer which will matter down the line.
The code I sent you assumes that the parameter is always pin=OFF1 because it's more a proof of concept

How to get result from AsyncTask without blocking UI thread?

I have external class extending AsyncTask to get string from website to parse it as JSONObject or JSONArray. Currently i am using method .get() to get the result, but app is dropping frames, while waiting for server to respond. I want to use it reusable because I am getting data from many different classes.
My Asynctask class:
public class JsonTask extends AsyncTask<String, String, String> {
protected String doInBackground(String...params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
Log.d("Response: ", "> Establishing Connection" );
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
Log.d("Response: ", "> " + line);
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}}
Now i am getting data by simply:
String result = new JsonTask().execute(url).get();
As per documentation for the get method:
Waits if necessary for the computation to complete, and then retrieves its result.
Therefore it will block the UI until it finishes the background task which will return the result.
You can create a listener that accepts the return value of the doInBackground in your JsonTask which the onPostExecute will call the listener.
I think you can read document again .
When an asynchronous task is executed, the task goes through 4 steps:
onPreExecute()
doInBackground(Params...)
onProgressUpdate(Progress...)
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

AsyncTask LIFX Bulb response

I have got problem with read output form request.
public JSONArray listLights()
{
try
{
URL adres = new URL("https://api.lifx.com/v1/lights/all");
HttpURLConnection polaczenie = (HttpURLConnection) adres.openConnection();
polaczenie.setRequestProperty("Authorization", "Bearer " + apiKey);
polaczenie.setRequestMethod("GET");
BufferedReader wejscie = new BufferedReader(new InputStreamReader((polaczenie.getInputStream())));
StringBuilder odpowiedz = new StringBuilder();
String json;
while ((json = wejscie.readLine()) != null)
odpowiedz.append(json);
wejscie.close();
return new JSONArray(odpowiedz.toString());
}
catch (Exception wyjatek)
{
wyjatek.printStackTrace();
}
return new JSONArray();
}
StackTrace
I added to AndroidManifest Internet access too.
Welcome to leave any comments. :P
EDIT:
I google internet and found partial solution. Added AsyncTask, but now I'm receiving '429' response code.
public class JSONTask extends AsyncTask<String, String, String>
{
String apiKey = "blah_blah_blah";
String txtresult;
#Override
protected String doInBackground(String... params) {
HttpsURLConnection connection = null;
BufferedReader reader = null;
try
{
URL adres = new URL(params[0]);
HttpsURLConnection polaczenie = (HttpsURLConnection) adres.openConnection();
polaczenie.setRequestProperty("Authorization", "Bearer " + apiKey);
polaczenie.setRequestMethod("GET");
System.out.println(polaczenie.getResponseCode());
InputStream stream = polaczenie.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null)
{
buffer.append(line);
}
return buffer.toString();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally {
if (connection != null)
connection.disconnect();
try
{
if (reader != null)
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
widok.setText(s);
}
}
My current StackTrace
EDIT2:
New day, new surprise. I figure out that I'm making connection with Bulb once/twice on every 10 attempts. Any ideas?
HTTP Status code 429 means too many requests in a given an amount of time. So how many requests exactly are you doing?
android.os.NetworkOnMainThreadException it means, that You have to make a htttp request from another threat than UIthread. Why are you using async task ?
Edit: You can also try make a call from postman and maybe You will see the problem.
In the end, everything is working. Problem was on the side of bulb or Lifx Cloud.

Android HttpURLConnection Error

I have an issue on my Android Project,
I want to call php page with get method and I use HttpURLConnection,
when I run this application give an error "app has stopped"
This is my code
public void someFunction() throws IOException {
String inputLine = "";
URL url = new URL("http://www.domain.com/demo.php?test=abc");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
result.append(line);
}
} finally{
in.close();
}
}
What should I do?
Thanks.
I have solved this issue with following code,
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Android is build to resist the developers to make network requests in the main (UI) thread. Changing Thread policy is not the solution. You should use AsyncTask to make all network requests.
public void someFunction() throws IOException {
AsyncTask task = new AsyncTask<Void,StringBuilder,StringBuilder>() {
#Override
protected StringBuilder doInBackground(Void... params) {
String inputLine = "";
URL url = new URL("http://www.domain.com/demo.php?test=abc");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
result.append(line);
}
} finally{
in.close();
}
return result;
}
#Override
protected void onPostExecute(StringBuilder result) {
super.onPostExecute(o);
}
};
task.execute();
}
See the android documentation: http://developer.android.com/reference/android/os/AsyncTask.html

Categories