Java tutorial
/* * Copyright (c) 2013. Saint Hsu(saint@aliyun.com) Hangzhou Taqi Tech Ltd * 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 org.flakor.androidtool.utils; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo.State; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.io.IOException; /** * http */ public class HttpUtil { public final static int WIFI_NET = 0X000002; public final static int MOBILE_NET = 0X000001; public final static int NONE_NET = 0X000000; private Context context; public HttpUtil(Context context) { this.context = context; } // Getrequest public static HttpGet getHttpGet(String url) { HttpGet request = new HttpGet(url); return request; } // Postrequest public static HttpPost getHttpPost(String url) { HttpPost request = new HttpPost(url); return request; } // ??response public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException { HttpResponse response = new DefaultHttpClient().execute(request); return response; } // ??response public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException { HttpResponse response = new DefaultHttpClient().execute(request); return response; } // ??Post? public static String queryStringForPost(String url) { // ?urlHttpPost HttpPost request = HttpUtil.getHttpPost(url); return queryStringForPost(request); } // ? public static String queryStringForPost(HttpPost request) { String result = null; try { // ? HttpResponse response = HttpUtil.getHttpResponse(request); // ?? if (response.getStatusLine().getStatusCode() == 200) { // ? result = EntityUtils.toString(response.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); result = "?"; } catch (IOException e) { e.printStackTrace(); result = "?"; } finally { return result; } } // ??Get? public static String queryStringForGet(String url) { // HttpGet HttpGet request = HttpUtil.getHttpGet(url); String result = null; try { // ? HttpResponse response = HttpUtil.getHttpResponse(request); // ?? if (response.getStatusLine().getStatusCode() == 200) { // ? result = EntityUtils.toString(response.getEntity()); return result; } } catch (ClientProtocolException e) { e.printStackTrace(); result = "?"; return result; } catch (IOException e) { e.printStackTrace(); result = "?"; return result; } return null; } public int checkNetworkState() { int state = NONE_NET; ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); // 3G?wifi?2G?????? if (mobile == State.CONNECTED || mobile == State.CONNECTING) { state = MOBILE_NET; } if (wifi == State.CONNECTED || wifi == State.CONNECTING) { state = WIFI_NET; } return state; } // private void showTips() { // AlertDialog.Builder builder = new AlertDialog.Builder(context); // builder.setIcon(android.R.drawable.ic_dialog_alert); // builder.setTitle(""); // builder.setMessage("..."); // builder.setPositiveButton("", new DialogInterface.OnClickListener() { // @Override // public void onClick(DialogInterface dialog, int which) { // // ? // context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); // } // }); // builder.setNegativeButton("?", new DialogInterface.OnClickListener() { // // @Override // public void onClick(DialogInterface dialog, int which) { // dialog.cancel(); // } // }); // builder.create(); // builder.show(); // } }