Java tutorial
/* * Copyright (c) 2012 Robot * * 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.zyf.bike.suzhou.utils; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class HttpOperation { public static final String TAG = "HttpOperation"; /** * Http Get?(?) * @param url * @return */ public static String doGetString(String url) { byte[] data = doGetBase(url, 3); if (data != null) { String re = new String(data); Logger.d("", "[Response]:" + re); return re; } else { return null; } } /** * Http Get?? * @param url * @param testnum ? * @return */ public static byte[] doGetBase(String url, int testnum) { if (testnum < 0) { return null; } try { //HttpClient HttpClient httpclient = new DefaultHttpClient(); //POST HttpGet get = new HttpGet(url); HttpResponse response = httpclient.execute(get); int code = response.getStatusLine().getStatusCode(); if (code == HttpStatus.SC_OK) { //? return EntityUtils.toByteArray(response.getEntity()); } else if (code == HttpStatus.SC_NOT_FOUND || code == HttpStatus.SC_FORBIDDEN || code == HttpStatus.SC_METHOD_NOT_ALLOWED) { //403 ? //404 ?? //405 ?? return null; } else { //?? doGetBase(url, testnum--); } } catch (Exception e) { Logger.e(TAG, e.getMessage(), e); } return null; } }