«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

올해는 머신러닝이다.

android HTTP GET,POST Examples 본문

Android/Tip&Tech

android HTTP GET,POST Examples

행복한 수지아빠 2011. 6. 10. 13:12

출처 : http://adsgear.tistory.com/49

HTTP GET
 

try {

        HttpClient client = new DefaultHttpClient();  

        String getURL = "http://www.google.com";

        HttpGet get = new HttpGet(getURL);

        HttpResponse responseGet = client.execute(get);  

        HttpEntity resEntityGet = responseGet.getEntity();  

        if (resEntityGet != null) {  

                    //do something with the response

                    Log.w("RESPONSE",EntityUtils.toString(resEntityGet));

                }

} catch (Exception e) {

    e.printStackTrace();

}


HTTP POST 

try {

        HttpClient client = new DefaultHttpClient();  

        String postURL = "http://somepostaddress.com";

        HttpPost post = new HttpPost(postURL); 

            List params = new ArrayList();

            params.add(new BasicNameValuePair("user", "kris"));

            params.add(new BasicNameValuePair("pass", "xyz"));

            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);

            post.setEntity(ent);

            HttpResponse responsePOST = client.execute(post);  

            HttpEntity resEntity = responsePOST.getEntity();  

            if (resEntity != null) {    

                Log.w("RESPONSE",EntityUtils.toString(resEntity));

            }

} catch (Exception e) {

        e.printStackTrace();

}


HTTP POST with File attachment 

File file = new File("path/to/your/file.txt");

try {

         HttpClient client = new DefaultHttpClient();  

         String postURL = "http://someposturl.com";

         HttpPost post = new HttpPost(postURL); 

     FileBody bin = new FileBody(file);

     MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  

     reqEntity.addPart("myFile", bin);

     post.setEntity(reqEntity);  

     HttpResponse response = client.execute(post);  

     HttpEntity resEntity = response.getEntity();  

     if (resEntity != null) {    

               Log.w("RESPONSE",EntityUtils.toString(resEntity));

         }

} catch (Exception e) {

    e.printStackTrace();

}

'Android > Tip&Tech' 카테고리의 다른 글

Fixed header in a TableLayout  (1) 2011.06.14
WORKING WITH JSON,XML AN ANDROID  (3) 2011.06.10
[펌]ViewFlipper 관한 설명(추천)  (0) 2011.06.09
progressbar 배경 및 색깔 바꾸기  (0) 2011.06.09
color등 기타팁등  (0) 2011.06.09