Java tutorial
/* * Copyright (C) 2012 kkurahar * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.kku.apps.pricesearch.util; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; public class HttpConnection { private static final String TAG = "HttpConnection"; public String doGet(String url) { HttpGet method = new HttpGet(url); DefaultHttpClient httpClient = new DefaultHttpClient(); method.setHeader("Connection", "Keep-Alive"); try { String resultRes = httpClient.execute(method, new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { // Xe?[^XR?[h int status = response.getStatusLine().getStatusCode(); // TODO v?C??@20120325 // yahooAPINGXg400badequest}?u if (HttpStatus.SC_BAD_REQUEST == status) { Log.d(TAG, "400 BAD REQUEST"); } else if (HttpStatus.SC_OK != status) { Log.d(TAG, "?MG?[??@status : " + status); //throw new RuntimeException("?MG?[?"); } return EntityUtils.toString(response.getEntity(), "UTF-8"); } }); return resultRes; } catch (ClientProtocolException e) { throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { httpClient.getConnectionManager().shutdown(); } } // // // URLBitmap // public static Bitmap getBitmapFromUrl(String urlParam) { // // try { // URL url = new URL(urlParam); // InputStream is = url.openConnection().getInputStream(); // return BitmapFactory.decodeStream(is); // } catch (MalformedURLException e) { // e.printStackTrace(); // // return null; // } catch (IOException e) { // e.printStackTrace(); // // return null; // } // // } // URLBitmap public static Bitmap getBitmapFromUrl(String url) { HttpGet method = new HttpGet(url); DefaultHttpClient httpClient = new DefaultHttpClient(); try { BufferedHttpEntity entity = httpClient.execute(method, new ResponseHandler<BufferedHttpEntity>() { @Override public BufferedHttpEntity handleResponse(HttpResponse response) throws ClientProtocolException, IOException { // Xe?[^XR?[h int status = response.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK != status) { throw new RuntimeException("?MG?[?"); } HttpEntity entity = response.getEntity(); BufferedHttpEntity bufferEntity = new BufferedHttpEntity(entity); return bufferEntity; } }); InputStream is = entity.getContent(); return BitmapFactory.decodeStream(is); } catch (ClientProtocolException e) { throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { httpClient.getConnectionManager().shutdown(); } } }