res/layout/httpclientdemo.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" >
<Button android:id="@+id/btnGet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnGet" android:text="GET" />
<Button android:id="@+id/btnPost" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnPost" android:text="POST" />
<Button android:id="@+id/btnUpload" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnUpload" android:text="UPLOAD" />
<Button android:id="@+id/btnDownload" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btnDownload" android:text="DOWNLOAD" />
</LinearLayout>
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" />
</LinearLayout> |
http://서버 IP:8888/WebApp/serverProc.jsp
<%@page import="java.net.URLDecoder"%> <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name"); String id = request.getParameter("id"); System.out.println("이름:"+name+", 아이디:"+id); %> <%= name %> <%=id %>
|
package android.test.app;
import java.net.URI; import java.util.*;
import org.apache.http.*; import org.apache.http.client.*; import org.apache.http.client.entity.*; import org.apache.http.client.methods.*; import org.apache.http.client.utils.URIUtils; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.*; import org.apache.http.message.*; import org.apache.http.protocol.*; import org.apache.http.util.*;
import android.app.*; import android.os.*; import android.util.*; import android.view.*; import android.widget.*; /* * AndroidManifest.xml 에 INTERNET 퍼미션을 설정했나요? */ public class HttpClientDemo extends Activity {
TextView tv; Button btnGet,btnPost,btnUpload,btnDownload; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.httpclientdemo); tv = (TextView) findViewById(R.id.textView1); btnGet = (Button)findViewById(R.id.btnGet); btnPost = (Button)findViewById(R.id.btnPost); btnUpload = (Button)findViewById(R.id.btnUpload); btnDownload = (Button)findViewById(R.id.btnDownload); }
private void httpRequestGet()throws Exception{ HttpClient client = new DefaultHttpClient(); //String url = "http://google.co.kr"; String url = "http://192.168.123.104:8888/WebApp/serverProc.jsp?name=Smith&id=smithid"; HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); HttpEntity resEntity = response.getEntity(); if(resEntity != null){ String res = EntityUtils.toString(resEntity); tv.setText(res); } }
/* 서버측으로 전달된 한글이 깨지는 문제.... */ private void httpRequestGet_2()throws Exception { List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("q", "httpclient")); qparams.add(new BasicNameValuePair("name", "김진호")); qparams.add(new BasicNameValuePair("id", "jinhokim")); qparams.add(new BasicNameValuePair("oq", null)); URI uri = URIUtils.createURI("http", "192.168.123.104", 8888, "/WebApp/serverProc.jsp", URLEncodedUtils.format(qparams, "UTF-8"), null); HttpGet get = new HttpGet(uri); HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get); HttpEntity resEntity = response.getEntity(); if(resEntity != null){ String res = EntityUtils.toString(resEntity); tv.setText(res); } } private void httpRequestPost() throws Exception { HttpClient client = new DefaultHttpClient(); /* 개발시에는 에뮬레이터의 호스트 컴퓨터 주소를 사용해야 한다. * localhost, 127.0.0.1 등은 모바일 기기에서 실행되는 웹서버를 의미하므로 안됨 */ String postUrl = "http://192.168.123.104:8888/WebApp/serverProc.jsp"; HttpPost post = new HttpPost(postUrl); List params = new ArrayList(); params.add(new BasicNameValuePair("name", "김진호")); params.add(new BasicNameValuePair("id", "jinhokim")); UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8); post.setEntity(ent); HttpResponse responsePost = client.execute(post); HttpEntity resEntity = responsePost.getEntity(); if(resEntity != null){ String res = EntityUtils.toString(resEntity); tv.setText(res); } } public void btnGet(View v){ try{ httpRequestGet_2(); }catch(Exception e){} } public void btnPost(View v){ try{ httpRequestPost(); }catch(Exception e){ Log.e("POST요청 에러", e.getMessage()); } } public void btnUpload(View v){ try{ httpRequestGet(); }catch(Exception e){} } public void btnDownload(View v){ try{ httpRequestGet(); }catch(Exception e){} } } |