Giridharan
Giridharan

Reputation: 4462

Cant able to call Async Task

I have an issue of calling AsyncTask.I am calling AsyncTask inside button but its not going for do in background and onPost execute. After clicking button nothing happens. Please help me to rectify mistake.

Photo.class

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View takephoto_view = inflater.inflate(R.layout.fragment_profile, null,false);
    img_profile = (ImageView)takephoto_view.findViewById(R.id.profile_imgphoto);
    but_takephoto = (Button)takephoto_view.findViewById(R.id.profile_buttakephoto);
    but_savephoto = (Button)takephoto_view.findViewById(R.id.profile_butsavephoto);
    but_takephoto.setOnClickListener(listener_takephoto);
    but_savephoto.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            firstmethod();
        }
    });

    return takephoto_view;
}


public void firstmethod() {
    new MyAsyn(getActivity()).execute();
}

public class MyAsyn extends AsyncTask<String, String, String> {
    Context con;

    public MyAsyn(Context con) {
        this.con = con;
    }

    @Override
    protected String doInBackground(String... params) {
        // Create a new HttpClient and Post Header
//          HttpClient httpclient = new DefaultHttpClient();
//         
//
//          StringBuilder sb;
//          try {
//              
//              StringEntity se = new StringEntity(json);
//             
//              httppost.setEntity(se);
//              System.out.print(json);
//              HttpResponse response = httpclient.execute(httppost);
//              if(response != null)
//              {
//                  InputStream is = response.getEntity().getContent();
//   
//                  BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//                  sb = new StringBuilder();
//   
//                  String line = null;
//                  try {
//                      while ((line = reader.readLine()) != null) {
//                          sb.append(line + "\n");
//                          
//                      }
//                      
//                      
//                  } catch (IOException e)
//                  {
//                      e.printStackTrace();
//                  } 
//                  finally {
//                      try {
//                          is.close();
//                      } catch (IOException e) {
//                          e.printStackTrace();
//                      }
//                  }
//                  
//                  
//                  
//                 
//              }
//   
//              
//          }catch (ClientProtocolException e) {
//              // TODO Auto-generated catch block
//          } catch (IOException e) {
//              // TODO Auto-generated catch block
//          }
            return null;
        }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Toast.makeText(getActivity(), "Hello", Toast.LENGTH_LONG).show();
    }

}

Upvotes: 2

Views: 90

Answers (1)

Nikhilesh Patve
Nikhilesh Patve

Reputation: 1870

Use this for Async task In Android:

To call Async Task Use this from any method:

List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("key1", "value1"));
    params.add(new BasicNameValuePair("key1", "value2"));
    new WEBSERVICEREQUESTOR(URL, params).execute();

Make this as member of class :

class WEBSERVICEREQUESTOR extends AsyncTask<String, Integer, String>
{

    String URL;
    List<NameValuePair> parameters;

    private ProgressDialog pDialog;

    public WEBSERVICEREQUESTOR(String url, List<NameValuePair> params)
    {
        this.URL = url;
        this.parameters = params;
    }

    @Override
    protected void onPreExecute()
    {
        pDialog = new ProgressDialog(LoginActivity.this);
        pDialog.setMessage("Processing Request...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params)
    {
        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            HttpPost httpPost = new HttpPost(URL);

            if (parameters != null)
            {
                httpPost.setEntity(new UrlEncodedFormEntity(parameters));
            }
            httpResponse = httpClient.execute(httpPost);

            httpEntity = httpResponse.getEntity();
            return EntityUtils.toString(httpEntity);

        }  catch (Exception e)
        {

        }
        return "";
    }

    @Override
    protected void onPostExecute(String result)
    {
        pDialog.dismiss();

        try
        {

        } catch (Exception e)
        {

        }
        super.onPostExecute(result);
    }

}

Upvotes: 1

Related Questions