用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

NetWorkUtil.

2014-07-07 作者: 云代码会员举报

[android]代码库

package com.bawei.sale8shop.net;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

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.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

public class NetWorkUtil {
	private static String result;
	/**
	 * 
	 * 方法说明:判断是否开启网络
	 *
	 * @param mContext
	 * @return
	 */
	public static boolean isNetwork(Context mContext){
		ConnectivityManager manager=(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info=manager.getActiveNetworkInfo();
		if(info==null||!info.isAvailable()){
			return false;
		}
		return true;
	}
	/**
	 * 
	 * 方法说明:访问网络接口,接到json数据
	 *
	 * @param mContext
	 * @param url
	 * @param jsonparams
	 * @return
	 */
	public static void getJsonData(Context mContext,String url,String jsonparams,final LoadJson loadjson){
		AsyncHttpClient client=new AsyncHttpClient();
		StringEntity entity;
		try {
			entity = new StringEntity(jsonparams, "utf-8");
			client.post(mContext, url, entity, "application/json",new JsonHttpResponseHandler(){
				@Override
				public void onSuccess(int statusCode, JSONObject response) {
					// TODO Auto-generated method stub
					super.onSuccess(statusCode, response);
					result=response.toString();
					loadjson.getJson(response.toString());
					Log.d("TAG", "请求数据成功"); 
				}
				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					super.onStart();
					Log.d("TAG", "开始请求数据");
				}
				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					super.onFinish();
					Log.d("TAG", "请求数据结束");
				}
				@Override
				public void onRetry() {
					// TODO Auto-generated method stub
					super.onRetry();
					Log.d("TAG", "重试");
				}
				@Override
				public void onFailure(Throwable e, JSONArray errorResponse) {
					// TODO Auto-generated method stub
					super.onFailure(e, errorResponse);
					Log.d("TAG", "失败");
				}
			});
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
//	public static 
	/**
	 * HttpClient访问网络接口 (暂时)
	 * @param url
	 * @param jsonparams
	 * @return
	 *	@param
	 *	@throws 
	 *	@return
	 */
	public static String getHttpClientJsonData(String url,String jsonparams){
		
		HttpPost httpPost = new HttpPost(url);
		StringEntity entity;
		try {
			entity = new StringEntity(jsonparams, HTTP.UTF_8);
			entity.setContentType("application/json");
			httpPost.setEntity(entity);
			HttpClient client = new DefaultHttpClient();
			HttpResponse response = client.execute(httpPost);
			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
				HttpEntity resEntity=response.getEntity();
				result=EntityUtils.toString(resEntity, "utf-8");
			}else{
				
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result ;
	}
	
	
	
	/**
	 * 通过地址获取Bitmap (暂时)
	 * @param imageUrl
	 * @return
	 *	@param
	 *	@throws 
	 *	@return
	 */
	public static Bitmap loadImageFromUrl(String imageUrl){
		Bitmap result = null;
		HttpGet req = new HttpGet(imageUrl);
		HttpParams connParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(connParams, 5 * 1000);
		HttpConnectionParams.setSoTimeout(connParams, 5 * 1000);
		HttpClient client = new DefaultHttpClient(connParams);
		
		try {
			HttpResponse resp = client.execute(req);
			if(resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				HttpEntity respEntity = resp.getEntity();
				result = BitmapFactory.decodeStream(respEntity.getContent());
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
		
	}
	public interface LoadJson{
		void getJson(String data);
	}
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...