2014-10-20 4 views
1

doInBackground 메서드의 외부 클래스에서 가져온 값을 사용하려고합니다.HttpAsyncTask의 외부 클래스에서 가져온 값을 사용하십시오.

나는 모든 것을 onCreate()에서 수행했지만, 그 값을 얻는 올바른 방법은 무엇입니까?

어쨌든 다른 요청을 할 수도 있습니다. 그런 다음 이것을 AsyncTask를 한 후

public class Client{ 
    public CLient(String email, String phNum){ 
     this.email = email; 
     this.phNum = phNum; 
    } 
} 

같은 클라이언트 클래스를 생성

HttpAsyncTask.execute(client); 

처럼 실행 호출에 해당 값을 전달할 수

public class MenuActivity extends Activity { 

    cUserData ud = new cUserData(this); //This is my external class 

    String email; 

    String phone; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_menu); 



     Bundle bundle = this.getIntent().getExtras(); 

     ((myApp) this.getApplication()).setSomeVariable(bundle.getString("email")); 



     cUserData ud = new cUserData(this); 

     String email= ud.getEmail; //I get the values from the external class 

     String phone = ud.getTelephone(); 


     new HttpAsyncTask().execute();  
    } 



    private class HttpAsyncTask extends AsyncTask<String, Void, String> { 

     @Override 

     protected String doInBackground(String... urls) { 

      String mEmail = email; //These values are null 

      String mPhone = phone; 

     String url = "http://www.myUrl.com/app/get.aspx?email=" + mEmail + "&telephone=" + mPhone; 

      HttpClient Client = new DefaultHttpClient(); 

      HttpGet httpget = new HttpGet(url); 

      try { 

      Client.execute(httpget); 

      } catch (ClientProtocolException e) { 

      e.printStackTrace(); 

      } catch (IOException e) { 

      e.printStackTrace(); 

      } 

      return null; 

     } 

    } 

} 
+0

http://stackoverflow.com/questions/24752274/passing-parameters-in-httpasynctask-execute – Carsten

+0

ud.getEmail은 다른 클래스에서 호출하는 메서드입니다. – imj

+0

public String getTelephone() { TelephonyManager telephonyManager; String context = Context.TELEPHONY_SERVICE; telephonyManager = (TelephonyManager) mContext.getSystemService (context); String mPhoneNumber = telephonyManager.getLine1Number(); return mPhoneNumber @ Archie.bpgc – imj

답변

1
public class MenuActivity extends Activity { 

cUserData ud = new cUserData(this); //This is my external class 

String email; 

String phone; 

@Override 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_menu); 



    Bundle bundle = this.getIntent().getExtras(); 

    ((myApp) this.getApplication()).setSomeVariable(bundle.getString("email")); 



    cUserData ud = new cUserData(this); 

    String email= ud.getEmail; //I get the values from the external class 

    String phone = ud.getTelephone(); 

    String mDataUrl ="www.myurl.com"; 
    mDataUrl += "?email=" + email; 
    mDataUrl += "&telephone=" + phone; 


    new HttpAsyncTask().execute(mDataUrl);  
} 



private class HttpAsyncTask extends AsyncTask<String, Void, String> { 

    @Override 

    protected String doInBackground(String... urls) { 
     return getData(urls[0]); 
    } 
} 

public static String getData(String dataUrl){ 
//NOW I CAN USE VALUES 
} 

} 
0

: 여기

내 코드입니다 AsyncTask를 확장하도록 약간 변경해야합니다.

내가 당신이라면
0

, 나는 다음과 같은 AsyncTask를을 사용합니다 :

public class MenuActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_menu); 


     Bundle bundle = this.getIntent().getExtras(); 

     ((LennyTracer) this.getApplication()).setSomeVariable(bundle.getString("email")); 

     new HttpAsyncTask().execute(new cUserData(this)); 
    } 

    private class HttpAsyncTask extends AsyncTask<cUserData, Void, String> { 

     @Override 

     protected String doInBackground(cUserData[] cUserDatas) { 

      String mEmail = cUserDatas.getEmail(); 

      String mPhone = cUserDatas.getTelephone(); 

      String url = "http://www.myUrl.com/app/get.aspx?email=" + mEmail + "&telephone=" + mPhone; 

      HttpClient Client = new DefaultHttpClient(); 

      HttpGet httpget = new HttpGet(url); 

      try { 

       Client.execute(httpget); 

      } catch (ClientProtocolException e) { 

       e.printStackTrace(); 

      } catch (IOException e) { 

       e.printStackTrace(); 

      } 

      return null; 

     } 

    } 
} 

그러나보다 효율적으로 프로세스를 처리하는 비동기 HTTP 요청에 대한 많은 도서관이 있습니다. 그 도서관에서보세요 :

http://loopj.com/android-async-http/

http://projects.spring.io/spring-android/

http://developer.android.com/training/volley/index.html

관련 문제