HTTP 연결 테스트 모듈 만들기
입력창 설정
입력창 설정
txtHost = (EditText)findViewById(R.id.host); txtPort = (EditText)findViewById(R.id.port); txtUrl = (EditText)findViewById(R.id.url); txtResult = (TextView)findViewById(R.id.content);버튼 달기
//액션 버튼 final Button btnFetch1 = (Button)findViewById(R.id.button1); final Button btnFetch2 = (Button)findViewById(R.id.button2); final Button btnFetch3 = (Button)findViewById(R.id.button3); btnFetch1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ txtResult.setText("");버튼 클릭 설정 (일부분)
btnFetch3.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ mPostAsyncTask = new PostAsyncTask().execute( txtHost.getText().toString().trim() ,txtPort.getText().toString().trim() ,txtUrl.getText().toString().trim() ); //getRequest(txtResult,txtUrl); } });AsyncTask 만들기 (비동기 요청/ 쓰레드 )
private class PostAsyncTask extends AsyncTask{ String resultString=null; HttpHost webhost; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Long doInBackground(String... strData) { long totalTimeSpent = 0; int numberOfParams = strData.length; //for(int i=0; i<2; i++) { //SystemClock.sleep(1000); //totalTimeSpent += new Long(strData[i]); //} HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. int timeoutConnection = 3000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = 3000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient client = new DefaultHttpClient(httpParameters); webhost = new HttpHost(strData[0],Integer.parseInt(strData[1]) ); //HttpGet get = new HttpGet("/list"); HttpGet request = new HttpGet(strData[2]); try{ //HttpResponse 서버 연결 주소 포트 response = client.execute(webhost, request); resultString = HttpHelper.request(response); } catch(Exception ex){ resultString = "Failed!"; //txtResult.setText("Failed!"); Log.d(TAG," PostAsyncTask Exception : "+ ex ); } return totalTimeSpent; } @Override protected void onProgressUpdate(Integer... progress) { } protected void onPostExecute(Long result) { // 종료 // End txtResult.setText(resultString); } @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); } }
'0.일반개발' 카테고리의 다른 글
안드로이드 플로요 2.2 (0) | 2010.09.17 |
---|---|
갤럭시S 프로요 업그레이드 (0) | 2010.09.17 |
안드로이드 HTTP 접근 여러방법들... (0) | 2010.09.16 |
애플 에어 프린트 ^^ 와우~~~ (0) | 2010.09.16 |
SQLite3 (0) | 2010.09.16 |