마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

AsyncTask 에서 메인 클래스의 변수를 어떻게 받을 수 있나요??ㅜㅠ

0 추천

안녕하세요.

메인

----------------------------------

static String Encryptionen; 

Encryptionen = new String(enDataByte);

new SendPost().execute();  

----------------------------------

----------------send post --------

public class SendPost extends AsyncTask<Void, Void, String> {
	static String Encryptionen;
    protected ArrayList<Map<String, String>> dataList;


	protected String doInBackground(Void... unused) {
		dataList = new ArrayList<Map<String,String>>();
		System.out.println("Encryption--->doInBackground--"+dataList);	
		System.out.println("Encryption--->doInBackground--"+Encryptionen);	
		//String content = executeClient("1","2","3");
		//return content;
		return null;
		
	}

	protected void onPostExecute(String result) {
		// 모두 작업을 마치고 실행할 일 (메소드 등등)
	}


	// 실제 전송하는 부분
	@SuppressWarnings("deprecation")
	public static String executeClient(String Encryption, String inputkey,String inputstr) {
		//String Encryption, String inputkey,String inputstr
		System.out.println("Encryption--->executeClient--");			
		ArrayList<NameValuePair> post = new ArrayList<NameValuePair>();
		
		System.out.println("Encryption--->executeClient--"+Encryption);
		System.out.println("Encryption--->executeClient--"+inputkey);	
		System.out.println("Encryption--->executeClient--"+inputstr);	
		post.add(new BasicNameValuePair("Encryption",Encryption));
		post.add(new BasicNameValuePair("inputkey", inputkey));
		post.add(new BasicNameValuePair("inputstr", inputstr));
		
		
		// 연결 HttpClient 객체 생성
		HttpClient client = new DefaultHttpClient();

		// 객체 연결 설정 부분, 연결 최대시간 등등
		HttpParams params = client.getParams();
		HttpConnectionParams.setConnectionTimeout(params, 5000);
		HttpConnectionParams.setSoTimeout(params, 5000);
		
		// Post객체 생성
		HttpPost httpPost = new HttpPost("http://xxxx.xxx.xxx/banking.php");
		
		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(post,"UTF-8");
			httpPost.setEntity(entity);
			client.execute(httpPost);
			return EntityUtils.getContentCharSet(entity);
		} catch (ClientProtocolException e) {
			System.out.println("Encryption--------------------------------err1-------------"+e);
			e.printStackTrace();
		}catch (IOException e) {
			System.out.println("Encryption----------------------------err2------------"+e);
			e.printStackTrace();
		}
		
		
		return null;
	}
}

------------------------------------

이렇게 했습니다.

Encryption 라는 변수의 값을 받아야 하는데 잘 안되서 다른 방법을 질문드립니다. ㅜㅠ 

변수에 static 을 줘도 사용을 못하고.

public String test(){
		return Encryptionen;
	}

 

메소드를 호출하게 해볼려고 해도 안되서.. 어떻게 하면 되는건지.. 도와주세요.

 

감사합니다.

익명사용자 님이 2015년 9월 3일 질문

2개의 답변

+1 추천
전역 변수를 주거나, AsyncTask 생성 시 파라메터로 주는 것도 가능하긴 한데,

 생성자에서 파라메터를 설정해서 멤버 변수로 설정한 후 실제 사용하는 곳에서 멤버 변수를 호출하는 씩으로 하시는게 간단할 듯 합니다.

private String mEncryption;
public SendPost(String encryption)
{
  mEncryption = encryption;
}
익명사용자 님이 2015년 9월 3일 답변
0 추천
// 실제 전송하는 부분
@Override
	protected String doInBackground(String... params) {
        return null 말고 encryption 변수값 리턴하세요
    }

//받을 화면
SendPost sendPost = new .....();
 
#1
//asycTask 실행
sendPost.execute(....);
 
//asyncTask 결과값
String rtnValue = sendPost.get();
 
#2
String rtnVlaue = sendPost.execute(...).get();

http://developer.android.com/reference/android/os/AsyncTask.html#get%28%29

 

bangbang (8,820 포인트) 님이 2015년 9월 3일 답변
bangbang님이 2015년 9월 3일 수정
...