无法调用异步任务

时间:2014-09-06 04:48:44

标签: android android-fragments android-asynctask

  

我有一个调用AsyncTask的问题。我在里面调用AsyncTask   按钮,但它不会在后台执行和onPost执行。   点击按钮后没有任何反应。请帮我纠正   错误。

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();
    }

}

1 个答案:

答案 0 :(得分:1)

将此用于Android中的异步任务:

调用异步任务从任何方法使用此方法:

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

将此作为班级成员:

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);
    }

}