2014-09-06 68 views
2

我有一个调用AsyncTask.I的问题,我打电话给 内的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

什么是logcat? – 2014-09-06 04:53:47

+0

@LittleChild:没有错误显示,但过了一段时间后,它会去AsyncTask。我不知道为什么它需要一些时间来输入 – Giridharan 2014-09-06 04:55:07

+0

如果你设置断点,会发生什么? FWIW,我从来不会在AsyncTasks中调用'super',但不应该打破它。 – 323go 2014-09-06 04:58:07

回答

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

} 
+0

完美的方法......非常感谢你NIK ........... – 2014-09-06 06:04:32