okay so i created a inner class which extends AsycTask in order for my code to run outwith the UI thread. However i'm getting this error so i assume this means some part of my onPostExecute needs to be done in doInBackground however i cant figure out exactly what this is
public class asyncTask extends AsyncTask<String, Integer, String> {
ProgressDialog dialog = new ProgressDialog(PetrolPriceActivity.this);
#Override
protected void onPreExecute() {
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.setMax(100);
dialog.setMessage("loading...");
dialog.show();
}
#Override
protected String doInBackground(String...parmans){
{
for(int i = 0; i < 100; i++){
publishProgress(1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String urlString = petrolPriceURL;
String result = "";
InputStream anInStream = null;
int response = -1;
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
return null;
}
URLConnection conn = null;
try {
conn = url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
// Check that the connection can be opened
if (!(conn instanceof HttpURLConnection))
try {
throw new IOException("Not an HTTP connection");
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
try
{
// Open connection
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
// Check that connection is OK
if (response == HttpURLConnection.HTTP_OK)
{
// Connection is OK so open a reader
anInStream = httpConn.getInputStream();
InputStreamReader in= new InputStreamReader(anInStream);
BufferedReader bin= new BufferedReader(in);
// Read in the data from the RSS stream
String line = new String();
while (( (line = bin.readLine())) != null)
{
result = result + "\n" + line;
}
}
}
catch (IOException ex)
{
try {
throw new IOException("Error connecting");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
}
#Override
protected void onProgressUpdate(Integer...progress){
dialog.incrementProgressBy(progress[0]);
}
#Override
protected void onPostExecute(String result) {
// Get the data from the RSS stream as a string
errorText = (TextView)findViewById(R.id.error);
response = (TextView)findViewById(R.id.title);
try
{
// Get the data from the RSS stream as a string
result = doInBackground(petrolPriceURL);
response.setText(result);
Log.v(TAG, "index=" + result);
}
catch(Exception ae)
{
// Handle error
errorText.setText("Error");
// Add error info to log for diagnostics
errorText.setText(ae.toString());
}
if(dialog.getProgress() == dialog.getMax())
dialog.dismiss();
}
}
if someone could point out my error as well as show an example of where the code is suppose to go in my doInBackground that would be great. Thanks
problem:
result = doInBackground(petrolPriceURL);
you are implicitly calling the doInbackground method in the onPostExecute which will actually run in your UI thread instead on a different thread thus resulting to Android:NetworkOnMainThreadException.
Also it is unnecessary to call doInBackground that it is already executed before onPostExecute when you execute your Asynctask. Just directly use the result parameter of the onPostExecute.
sample:
#Override
protected void onPostExecute(String result) {
// Get the data from the RSS stream as a string
errorText = (TextView)findViewById(R.id.error);
response = (TextView)findViewById(R.id.title);
response.setText(result);
if(dialog.getProgress() == dialog.getMax())
dialog.dismiss();
}
I suspect the error is related to this part of your code:
try
{
// Get the data from the RSS stream as a string
result = doInBackground(petrolPriceURL);
response.setText(result);
Log.v(TAG, "index=" + result);
}
doInBackgound is called automatically when you call asynctask.execute. To start your task correctly you should (1) create a new instance of your task; (2) pass the string params you need to use in doInBackground in the execute method; (3) use them; (4) return the result to onPostExecute.
For Example:
//in your activity or fragment
MyTask postTask = new MyTask();
postTask.execute(value1, value2, value3);
//in your async task
#Override
protected String doInBackground(String... params){
//extract values
String value1 = params[0];
String value2 = params[1];
String value3 = params[2];
// do some work and return result
return value1 + value2;
}
#Override
protected void onPostExecute(String result){
//use the result you returned from you doInBackground method
}
You should try to do all of your "work" in the doInBackground method. Reutrn the result you want to use on the main/UI thread. This will automaticlly be passed as an argument to the onPostExecute method (which runs on the main/UI thread).
Related
In my code,first I access an address and I got the text file. In that, there are many picture links, such as http://dnight-math.stor.sinaapp.com/%E5%9C%B0%E7%90%861_img004.jpg. I use regular expression to find all the links to make a arraylist. Then I use downloadService to download all the pictures. When I first press a button to download ,it can run successfully. But it doesn't work if the button is pressed again and throws error. I think this bug is about thread but I don't know how to solve it.
HttpUtil.sendHttpRequest(address,
new HttpCallbackListener() {
#Override
public void onFinish(String response) {
try {
ArrayList<String> urlList = new ArrayList<>();
Pattern p = Pattern.compile("http:.*?.com/(.*?.(jpg|png))");
Matcher m = p.matcher(response);
StringBuffer buffer = new StringBuffer();
while (m.find()) {
m.appendReplacement(buffer, "<T>" + + m.group(1) + "</T>");
urlList.add(m.group());
}
m.appendTail(buffer);
response = buffer.toString();
Message m2 = Message.obtain();
m2.obj = response;
m2.what = 1;
mHandler.sendMessage(m2);
new DownloadService("/data/data/com.baodian/files",
urlList,
new DownloadStateListener() {
#Override
public void onFinish() {
}
#Override
public void onFailed() {
}
}, context).startDownload();
;
// JSONObject singleChoice=all.getjson
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onError(Exception e) {
}
});
public class HttpUtil {
public static void sendHttpRequest(final String address,
final HttpCallbackListener listener) {
new Thread(new Runnable() {
#Override
public void run() {
HttpURLConnection connection=null;
try {
URL url=new URL(address);
connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream in=connection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(in,"gbk"));
StringBuilder response=new StringBuilder();
String line=null;
while ((line=reader.readLine())!=null) {
response.append(line);
}
if (listener!=null) {
listener.onFinish(response.toString());
}
} catch (Exception e) {
if (listener != null) {
listener.onError(e);
}
}
}
}).start();
}
}
If you look at SimY4's answer here,
he says that the error you're getting "means the thread pool is busy and queue is full as well".
What you currently do is call onFailed when you encounter the error. What you can do is implement
a supplementary enqueing scheme. You can cache the newer urls until the thread queue has space, create and enqueue
the new threads at that point.
The following thread might prove useful : Java executors: how to be notified, without blocking, when a task completes?
I added in the MainActivity a button click event:
public void addListenerOnButton()
{
btnClick = (Button) findViewById(R.id.checkipbutton);
btnClick.setOnClickListener(new OnClickListener()
{
byte[] response = null;
#Override
public void onClick(View arg0)
{
text = (TextView) findViewById(R.id.textView2);
Thread t = new Thread(new Runnable()
{
#Override
public void run()
{
for (int i = 0; i < ipaddresses.length; i++)
{
try
{
response = Get(ipaddresses[i]);
break;
} catch (Exception e)
{
text.setText("Connection Failed");
}
}
if (response!=null)
{
String a = null;
try
{
a = new String(response,"UTF-8");
text.setText(a);
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
Logger.getLogger("MainActivity(inside thread)").info(a);
}
}
});
t.start();
}
});
}
I wanted to create a break when it's entering the try block after doing the response = Get(ipaddresses[i]); in order to stop the for loop.
The problem is that after it's done the response = Get(ipaddresses[i]); when it's supposed to be doing the break, my program crashes.
On the android device I get the message:
unfortunately myapp has stopped
And when I click ok on the message the program just closes.
I can't figure out why the break makes the program crash.
This is the Get method:
private byte[] Get(String urlIn)
{
URL url = null;
String urlStr = urlIn;
if (urlIn!=null)
urlStr=urlIn;
try
{
url = new URL(urlStr);
} catch (MalformedURLException e)
{
e.printStackTrace();
return null;
}
HttpURLConnection urlConnection = null;
try
{
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
byte[] buf=new byte[10*1024];
int szRead = in.read(buf);
byte[] bufOut;
if (szRead==10*1024)
{
throw new AndroidRuntimeException("the returned data is bigger than 10*1024.. we don't handle it..");
}
else
{
bufOut = Arrays.copyOf(buf, szRead);
}
return bufOut;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
finally
{
if (urlConnection!=null)
urlConnection.disconnect();
}
}
The reason for the crash is most likely apparent from the stacktrace that you haven't shown us.
But the logic of that loop is pretty dubious ... to me.
Without the break, the loop iterates over all of the IP addresses, and tries Get on each one. At the end, response will be the last value returned by a Get call, which may or may not be null.
With the break, the loop terminates after the first IP address for which Get doesn't throw an exception ... irrespective of what the Get call returns. (That could be null.)
These could be the cause of your crash, but it could be something else. Either way, the logic is suspicious. (And calling a method Get is bad style!)
UPDATE
Given that the Get method catches exceptions and returns null on failure, the recommended structure for the code that calls it is:
for (int i = 0; i < ipaddresses.length; i++) {
response = Get(ipaddresses[i]);
if (response != null) {
break;
}
}
if (response == null) {
// notify connection failed
} else {
// process response
}
There is not need for a "belt and braces" try {...} catch in the calling code ... if you have already dealt with the expected exceptions in Get. And (IMO) you should (almost) never catch Exception, because that is liable to conceal bugs.
i'm trying to check the value of 2 strings every minute, and close the Thread
when the value will be different , but this code freeze my UI. that's the code.
#Override
protected void onPostExecute(String result)
{
//Download sourcecode on String
String secondaChar = result;
//Strings declaration
String First = "";
String Second = "";
try
{
//Writing sourcecode on string
Write(secondaChar);
String ReadingFile="FileSorgente";
First=reading(ReadingFile);
while (true)
{
// Downloading new sourcecode on string
secondaChar = result.replaceAll("[^a-zA-Z]+", "");
//writing new SC on string
Scrittura(secondaChar);
// new SC on second string
Second = Reading(ReadingFile);
// check until 2 strings are the same
if(First.equalsIgnoreCase(Second))
{
Toast.makeText(getApplicationContext(), "Keep Checking", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "FALSE", Toast.LENGTH_LONG).show();
break;
}
Thread.sleep(60 * 1000);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
textView.setText("Usless");
}
Reading and Writing are 2 different method, i've test this in java befor and it worked, but probably in Android sdk i have to do something different.
You should execute long running tasks - for instance, Thread.sleep(...) - in the doInBackground() - which is running off the UI thread.
onPostExecute() is supposed to be only called after the doInBackground() completes, and is used exactly to update the UI - thus it runs on the UI thread. Every delay you make there will cause your app to become unresponsive.
To fix this, move your logic to doInBackground(). Remeber that all the UI operations, though, have to happen on the UI thread. So, for instance, showing toasts would have to be done from onPostExecute() or from onProgressUpdate().
See this excellent answer for an example on how to do this. Note that Thread.sleep() is called there as well, but in doInBackground().
#Override
protected void doInBackground(params....)
{
//Download sourcecode on String
String secondaChar = result;
//Strings declaration
String First = "";
String Second = "";
try
{
//Writing sourcecode on string
Write(secondaChar);
String ReadingFile="FileSorgente";
First=reading(ReadingFile);
while (true)
{
// Downloading new sourcecode on string
secondaChar = result.replaceAll("[^a-zA-Z]+", "");
//writing new SC on string
Scrittura(secondaChar);
// new SC on second string
Second = Reading(ReadingFile);
// check until 2 strings are the same
if(First.equalsIgnoreCase(Second))
{
publishProgress( "Keep Checking" );
}
else
{
publishProgress( "FALSE" );
break;
}
Thread.sleep(60 * 1000);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
publishProgress("Usless");
}
#Override
protected void onProgressUpdate(String... progress) {
showToast(progress[0]);
}
FTP code, but only the download...
public class ImageUploadTask extends AsyncTask <String, Void, String>{
String e;
protected String doInBackground(String...args) {
String strResponce = "";
ftpClient = new FTPClient();
s="finish";
try {
ftpClient.connect(InetAddress.getByName(SERVER));
ftpClient.login(USERNAME, PASSWORD);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
textTargetUri.setText("good");
final String remote = "/1.jpg";//FTP adress/filename
String savefilepath = "/sdcard/download"+remote;
File downloadfile = new File(savefilepath);//download
local = new FileOutputStream(downloadfile);
ftpClient.retrieveFile(remote, local);
local.close();
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
}
return s;
}
}
BUT!! FTP DownLoad code do insert CountingOutputStream function.
CountingOutputStream function is commons-io-2.4.jar.
project in commons-io-2.4.jar and commons-net-3.3.jar to there. T_T
public class ImageUploadTask extends AsyncTask <String, Void, String>{
String e;
protected String doInBackground(String...args) {
String strResponce = "";
ftpClient = new FTPClient();
s="finish";
try {
ftpClient.connect(InetAddress.getByName(SERVER));
ftpClient.login(USERNAME, PASSWORD);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
textTargetUri.setText("good");
final String remote = "/1.jpg";//FTP adress/filename
String savefilepath = "/sdcard/download"+remote;
File downloadfile = new File(savefilepath);//download
local = new FileOutputStream(downloadfile);
long fileSize = 0;
FTPFile[] files = ftpClient.listFiles(remote);
if (files.length == 1 && files[0].isFile()) {
fileSize = files[0].getSize();
String a = String.valueOf(fileSize);
Log.d("File Size", a);
}
CountingOutputStream cos = new CountingOutputStream(local) {
protected void beforeWrite(int n) {
super.beforeWrite(n);
int progress = Math.round((getCount() * 100) / 879394);
//String b = String.valueOf(progress);
//Log.d("File persent", b);
}
};
ftpClient.retrieveFile(remote, cos);
local.close();
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
}
return s;
}
}
What's wrong with the code, I do not know how?
code no error.
but APP start error.
In a background tread (such as in doInBackGround()) no UI elements can be changed at all. If in activity use runOnUiThread or else use textTargetUri.post. This way the setText part takes place on the UI thread as needed.
I'm still struggling to find an answer to my question. I want to download 3 strings for each item in the listview to the phone. I know how to get all the data from the server, just not how to append the data to the litview, I'm really annoyed and this problem is dragging me down.
My Code:
public class ChatService extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chatservice);
try {
ContactsandIm();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CheckLogin();
private void CheckLogin() {
// TODO Auto-generated method stub
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
/* login.php returns true if username and password is equal to saranga */
HttpPost httppost = new HttpPost("http://gta5news.com/login.php");
try {
// Execute HTTP Post Request
Log.w("HttpPost", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent())
.toString();
Log.w("HttpPost", str);
if (str.toString().equalsIgnoreCase("true")) {
Log.w("HttpPost", "TRUE");
try {Thread.sleep(250);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//put intent here(21/3/12);
} else {
Log.w("HttpPost", "FALSE");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
private void ContactsandIm() throws URISyntaxException, ClientProtocolException, IOException {
// TODO Auto-generated method stub
BufferedReader in = null;
String data = null;
HttpClient get = new DefaultHttpClient();
URI website = new URI("http://www.gta5news.com/test.php");
HttpGet webget = new HttpGet();
webget.setURI(website);
HttpResponse response = get.execute(webget);
Log.w("HttpPost", "Execute HTTP Post Request");
in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
//now we'll return the data that the PHP set from the MySQL Database.
if (in.equals("True")); {
Toast.makeText(this,"yay", Toast.LENGTH_LONG).show();
}
}
// end bracket for "ContactsandIm"
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
Use an ArrayAdapter on your list, and add items to this adapter, then call notifyDatasetChanged on it, and zou
First of all when we connect to Server using the Network Threads, then you should go for AsyncTask or Handlers. Which is specially used for handling such Threads.
ListView can be created by using default Listview ans also the Custom Listview where we can design our own Row design in the Row.xml and the same design will be used for all the rows.
If you want move forward or go for some advanced Listview then even we can use 'n' number of designs for different rows.
Here in your case, You should use a AsyncTask for fetching the data and use the Listview for displaying rows.
You can get more information from the below link.
https://docs.google.com/leaf?id=0B2qCFbmeiTFxN2ViZjVlOTUtNmY3ZS00NThhLTg3N2UtYjVkYjgyM2Y4MWUy&hl=en&authkey=COeP8JYN