0

When I wrote this code It gives NetworkOnMainThreadException so I want to implement it using AsyncTask Although I have seen many questions and answers about this Error but not able to Write it . I want to pass an argument to Asynctask class and want to assign returned data to a variable in the current class .Colud you please help me. Here is My code

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    int tem = 1940+position;
    String temp = ""+tem;
    String ReceivedData ="<";
    GettingInternetData something = new GettingInternetData(temp);
    try {
        ReceivedData = something.getMoviesData();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

GettingInternetData.java

public class GettingInternetData {

    String y = null;
    public GettingInternetData(String year) {
        // TODO Auto-generated constructor stub
        y = year;
    }
public String getMoviesData() throws Exception
{
    BufferedReader toRead = null;
    String data = null;
    try
    {
        HttpClient client = new DefaultHttpClient();
        URI url = new URI("URL goes here which requires an argument temp");
        HttpGet getting = new HttpGet();
        getting.setURI(url);
        HttpResponse resp = client.execute(getting);
        toRead = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
        StringBuffer alldata = new StringBuffer();
        String l ="";
        String nl = System.getProperty("line.Separator");
        while((l = toRead.readLine()) !=null)
        {
            alldata.append(l + nl);
        }
        toRead.close();
        data = alldata.toString();
        return data;
    }
    finally{
        if (toRead != null){
            try{
                toRead.close();
                return data;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
}
}

Thanks in Advance

K Guru
  • 1,292
  • 2
  • 17
  • 36
Dinesh Chitta
  • 670
  • 8
  • 23
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – 2Dee Jan 13 '15 at 15:23
  • Terrible question, duplicate of so many other similar ones, but this time you're saying "I know I have to use AsyncTask" but you don't show what you tried when using it, which is worse ... – 2Dee Jan 13 '15 at 15:24

3 Answers3

0

You'll want to pass arguments to your custom AsyncTask, have it do the work in the doInBackground() method and return any results. You can then process the results in the onPostExecute() method. For a more detailed explanation of AsyncTask you may find this article helpful: http://po.st/Cei3m2

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
0

try this

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    new InternetAsynTask().execute(String.valueOf(1940+position));
}

public static class InternetAsynTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        GettingInternetData gettingInternetData = new GettingInternetData(params[0]);
        return gettingInternetData.getMoviesData();
    }

    @Override
    protected void onPostExecute(String s) {
        // TODO:
        // processing receive data here
    }
}
alijandro
  • 11,627
  • 2
  • 58
  • 74
0

First, you should not call any service getting or posting information via internet from your application main thread. So, you can use android os provided asynctask.Also, you told that, you want to pass information into asynctask. I think it will help you, private class SyncWithServerTask extends AsyncTask<VALUE, Void, String> { protected Long doInBackground(VALUE... values) { int count = valus.length; for (int i = 0; i < count; i++) { publishProgress((int) ((i / (float) count) * 100)); //HERE YOUR CODE // Escape early if cancel() is called if (isCancelled()) break; } return result; }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(String result) {
     showDialog(result);
 }

}

then, call your service using, new SyncWithServerTask().execute(value1, value2, value3);

Ratna Halder
  • 1,954
  • 16
  • 17