(1) 안드로이드 1.0 HTTP GET 방식
Sending a multipart request first involves some environmental setup, since the required libraries are not bundled with Android 1.0. First, go to the Apache HttpClient download page and download the distribution called “Binary with dependencies”. In that package you’ll find two libraries: apache-mime4j-0.4.jar and httpmime-4.0-beta1.jar. Copy these files to e.g. the lib/ folder of your Android project and add them to the build path. You can now use a MultipartEntity to send multipart POSTs as such:
(2) 방식 main.xml
HttpClient httpClient = new DefaultHttpClient(); StringBuilder uriBuilder = new StringBuilder(SERVICE_ENDPOINT); uriBuilder.append("?param0=" + param0); uriBuilder.append("param1=" + param1); uriBuilder.append("paramN=" + paramN); HttpGet request = new HttpGet(uriBuilder.toString()); HttpResponse response = httpClient.execute(request); int status = response.getStatusLine().getStatusCode(); // we assume that the response body contains the error message if (status != HttpStatus.SC_OK) { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); response.getEntity().writeTo(ostream); Log.e("HTTP CLIENT", ostream.toString())); } else { InputStream content = response.getEntity().getContent(); //content.close(); // this will also close the connection }
Sending a multipart request first involves some environmental setup, since the required libraries are not bundled with Android 1.0. First, go to the Apache HttpClient download page and download the distribution called “Binary with dependencies”. In that package you’ll find two libraries: apache-mime4j-0.4.jar and httpmime-4.0-beta1.jar. Copy these files to e.g. the lib/ folder of your Android project and add them to the build path. You can now use a MultipartEntity to send multipart POSTs as such:
HttpClient httpClient = new DefaultHttpClient(); HttpPost request = new HttpPost(SERVICE_ENDPOINT); MultipartEntity entity = new MultipartEntity(); entity.addPart("param0", new StringBody("value0")); entity.addPart("param1", new StringBody(Double.toString(1.0))); entity.addPart("paramN", new FileBody(new File("/bar/baz"))); request.setEntity(entity); HttpResponse response = httpClient.execute(request); int status = response.getStatusLine().getStatusCode(); if (status != HttpStatus.SC_OK) { // see above } else { // see above }
(2) 방식 main.xml
'0.일반개발' 카테고리의 다른 글
[Android] EditText 이벤트 처리. (0) | 2010.09.14 |
---|---|
EditText 사용하기 (0) | 2010.09.14 |
안드로이드 앱, 리스트에 헤더 만들기 ( 구분 ) (2) | 2010.09.14 |
안드로이드 타원형 박스 (0) | 2010.09.14 |
날짜 안드로이드 자바 일수 계산 (0) | 2010.09.14 |