`
jenly
  • 浏览: 18250 次
文章分类
社区版块
存档分类
最新评论

Android网络通信的基本实现

 
阅读更多

似乎很久没有写博客了,今天无意间看了下半年前写的代码,设计似乎很有局限性,但基本的通信也算是实现了。

不知道以后看到现在写的代码会不会也会有这样的想法呢?

进入正题,android网络通信的基本实现的有两种方式,一种是通过HttpClient来实现,一种是通过HttpURLConnection来实现。

直接上代码:

首先是一个抽象的http类

/**
 * 抽象的Http
 * @author Jenly
 * @date 2014-8-7
 *
 */
public abstract class AbstractHttp {
	
	/** 默认编码  (UTF-8) */
	public static final String DEFAULT_ENCODING = "UTF-8";
    /** GET请求时 url和 参数的分隔符 */
    public static final String URL_AND_PARA_SEPARATOR = "?";
	/** 是否取消请求 */
    protected boolean isCancel = false;
    
	public static final int DEFAULT_BYTE_LENGTH = 8192;
	
	protected static final int LONG_TIME = 0;
	
	protected int connectionTimeOut = LONG_TIME;
	
	protected int soTimeOut = LONG_TIME;

	public enum HttpMethod{
		/** GET请求 */
		GET,	
		/** POST请求 */
		POST
	}
	
	/**
	 * 构造
	 */
	public AbstractHttp(){
		super();
	}
	
	public AbstractHttp(int connectionTimeOut,int soTimeOut){
		super();
		if(connectionTimeOut<0 || soTimeOut <0){
			throw new RuntimeException("connectionTimeOut<0 || soTimeOut<0");
		}
		
		if(connectionTimeOut>0)
			this.connectionTimeOut = connectionTimeOut;
		if(soTimeOut>0)
			this.soTimeOut = soTimeOut;
	}
	
	//-----------------------------------------
	
	/**
	 * 取消请求
	 */
	protected void cancel(){
		isCancel = true;
	}
	
	/**
	 * 异步线程
	 * @param runnable
	 */
	protected void asyncThread(Runnable runnable){
		new Thread(runnable).start();
	}
	
	
	/**
	 * 异步连接  默认GET请求
	 * @param url
	 * @param httpCallBack
	 */
	public abstract void asyncConnect(String url,HttpCallBack<String> httpCallBack);
	
	/**
	 * 异步连接  默认GET请求
	 * @param url
	 * @param params
	 * @param httpCallBack
	 */
	public abstract void asyncConnect(String url,Map<String,String> params,HttpCallBack<String> httpCallBack);
	/**
	 * 异步连接
	 * @param url
	 * @param params
	 * @param httpMethod
	 * @param httpCallBack
	 */
	public abstract void asyncConnect(String url,Map<String,String> params,HttpMethod httpMethod,HttpCallBack<String> httpCallBack);
	
	/**
	 * 同步连接  默认GET请求
	 * @param url
	 * @param httpEntity
	 * @param httpCallBack
	 */
	public abstract String syncConnect(String url);
	
	/**
	 * 同步连接  默认GET请求
	 * @param url
	 * @param params
	 */
	public abstract String syncConnect(String url,Map<String,String> params);
	
	/**
	 * 同步连接  默认GET请求
	 * @param url
	 * @param params
	 * @param httpMethod
	 */
	public abstract String syncConnect(String url,Map<String,String> params,HttpMethod httpMethod);
	
	/**
	 * 同步连接  默认GET请求
	 * @param url
	 * @param httpEntity
	 * @param httpCallBack
	 */
	public abstract String syncConnect(String url,Map<String,String> params,HttpCallBack<String> httpCallBack);
	
	/**
	 * 同步连接
	 * @param url
	 * @param params
	 * @param httpMethod
	 * @param httpCallBack
	 */
	public abstract String syncConnect(String url,Map<String,String> params,HttpMethod httpMethod,HttpCallBack<String> httpCallBack);
	
	
	/**
	 * 异步下载文件
	 * @param url
	 * @param fileName
	 * @param httpDownloadCallBack
	 * @return
	 */
	public abstract void asyncDownloadFile(String url,String fileName,HttpCallBack<File> httpDownloadCallBack);
	
	/**
	 * 同步下载文件
	 * @param url
	 * @param fileName
	 * @return
	 */
	public abstract File syncDownloadFile(String url,String fileName);
	
	/**
	 * 同步下载文件
	 * @param url
	 * @param fileName
	 * @param httpDownloadCallBack
	 * @return
	 */
	public abstract File syncDownloadFile(String url,String fileName,HttpCallBack<File> httpDownloadCallBack);
	
	
	
}


监听回调接口:

/**
 * Http连接回调接口
 * @author Jenly
 * @date 2014-8-7
 */
public interface HttpCallBack<T>{
	
	/**
	 * 开始
	 * @param url
	 */
	void onStart(String url);
	
	/**
	 * 加载…
	 * @param progress
	 * @param count
	 */
	void onLoading(long progress,long count);
	
	/**
	 * 成功
	 * @param t 返回的对象
	 */
	void onSuccess(T t);
	
	/**
	 * 失败
	 * @param error
	 * @param e
	 */
	void onFailure(String error,Throwable e);
	

	/**
	 * 取消
	 */
	void onCancel();
}


/**
 * Http连接请求回调
 * @author Jenly
 * @date 2014-8-7
 */
public abstract class HttpConnectCallBack implements HttpCallBack<String>{

	@Override
	public abstract void onSuccess(String content);
	
}


/**
 * Http下载回调
 * @author Jenly
 * @date 2014-8-7
 */
public abstract class HttpDownloadCallBack implements HttpCallBack<File>{

	@Override
	public abstract void onSuccess(File file);
}

通过HttpClient实现的方式

/**
 * Http网络通信(httpClient实现)
 * @author Jenly
 * @date 2014-8-7
 */
public class Http extends AbstractHttp{
	

	public Http() {
		super();
	}

	public Http(int connectionTimeOut, int soTimeOut) {
		super(connectionTimeOut, soTimeOut);
	}


	/**
	 * 获得一个HttpClient
	 * @return
	 */
	private HttpClient getHttpClient(){
		
		HttpParams httpParams = new BasicHttpParams();
		
		if(connectionTimeOut != LONG_TIME && soTimeOut != LONG_TIME){
			HttpConnectionParams.setConnectionTimeout(httpParams,connectionTimeOut);
			HttpConnectionParams.setSoTimeout(httpParams, soTimeOut);
		}
		
		HttpConnectionParams.setTcpNoDelay(httpParams, true);
		HttpConnectionParams.setSocketBufferSize(httpParams,DEFAULT_BYTE_LENGTH);

		HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

		SchemeRegistry schemeRegistry = new SchemeRegistry();
		schemeRegistry.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		schemeRegistry.register(new Scheme("https", SSLSocketFactory
				.getSocketFactory(), 443));
		ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager(
				httpParams, schemeRegistry);
		
		return new DefaultHttpClient(threadSafeClientConnManager,httpParams);
	}
	
	
	/**
	 * 获得HttpUriRequest
	 * @param url
	 * @param httpMethod
	 * @param params
	 * @return
	 */
	private HttpUriRequest getHttpUriRequest(String url,HttpMethod httpMethod,Map<String,String> params){
		
		if(HttpMethod.POST == httpMethod){
			
			HttpPost httpPost = new HttpPost(url);
			if(params!=null){
				List<NameValuePair> list = new ArrayList<NameValuePair>();
				for(String key : params.keySet()){
					list.add(new BasicNameValuePair(key, params.get(key)));
				}
				try {
					httpPost.setEntity(new UrlEncodedFormEntity(list,DEFAULT_ENCODING));
				} catch (UnsupportedEncodingException e) {
					LogUtils.e(e);
				}
			}
			return httpPost;
		}else{
			if(params!=null){
				List<NameValuePair> list = new ArrayList<NameValuePair>();
				
				for(String key : params.keySet()){
					list.add(new BasicNameValuePair(key, params.get(key)));
				}
				url += URL_AND_PARA_SEPARATOR;
				
				url += URLEncodedUtils.format(list,DEFAULT_ENCODING);
			}
			
			LogUtils.v("GET:"+ url);
			HttpGet httpGet = new HttpGet(url);
			
			return httpGet;
		}
		
		
	}

	@Override
	public void asyncConnect(String url,HttpCallBack<String> httpCallBack) {
		asyncConnect(url, null, HttpMethod.GET, httpCallBack);
	}
	
	@Override
	public void asyncConnect(String url, Map<String,String> params,HttpCallBack<String> httpCallBack) {
		asyncConnect(url, params, HttpMethod.GET, httpCallBack);
	}
	
	@Override
	public void asyncConnect(final String url,final Map<String,String> params,final HttpMethod httpMethod,final HttpCallBack<String> httpCallBack) {
		asyncThread(new Runnable(){
			@Override
			public void run() {
				syncConnect(url, params,httpMethod, httpCallBack);
			}
		});
	}
	
	@Override
	public String syncConnect(String url) {
		return syncConnect(url, null, HttpMethod.GET, null);
	}
	
	@Override
	public String syncConnect(String url, Map<String,String> params) {
		return syncConnect(url, params, HttpMethod.GET, null);
	}
	
	@Override
	public String syncConnect(String url, Map<String,String> params,HttpCallBack<String> httpCallBack) {
		return syncConnect(url, params, HttpMethod.GET, httpCallBack);
	}
	
	@Override
	public String syncConnect(String url, Map<String,String> params,HttpMethod httpMethod) {
		return syncConnect(url, params,httpMethod, null);
	}

	@Override
	public String syncConnect(String url, Map<String,String> params,HttpMethod httpMethod,HttpCallBack<String> httpCallBack) {
		
		if(TextUtils.isEmpty(url)){
			return null;
		}
		
		BufferedReader reader = null;
		
		HttpClient httpClient = null;
		
		int statusCode = -1;
		try {
			LogUtils.v(url);
			
			if(httpCallBack != null)
				httpCallBack.onStart(url);
			
			HttpUriRequest request = getHttpUriRequest(url,httpMethod,params);
			httpClient = getHttpClient();
			HttpResponse httpResponse = httpClient.execute(request);
			statusCode = httpResponse.getStatusLine().getStatusCode();
			if(statusCode==HttpStatus.SC_OK){
				
				reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
				
				StringBuffer buffer = new StringBuffer();
				String line = null;
				
				long progress = 0;
				
				long count = httpResponse.getEntity().getContentLength();
				isCancel = false;
				if(httpCallBack != null && count!=-1)
					httpCallBack.onLoading(progress, count);
				while ((!isCancel) && (line = reader.readLine())!=null) {
					buffer.append(line);
					
					if(httpCallBack != null && count!=-1){
						progress+= line.getBytes().length;
						httpCallBack.onLoading(progress, count);
					}
				}
				
				
				if(httpCallBack != null){
					if(!isCancel){
						progress = count;
						httpCallBack.onLoading(progress, count);
					}else{
						reader.close();
						httpCallBack.onCancel();
						return null;
					}
				}
				reader.close();
				if(httpCallBack != null && !isCancel)
					httpCallBack.onSuccess(buffer.toString());
				
				if(httpClient!=null)
					httpClient.getConnectionManager().shutdown();
				
				return buffer.toString();
			}else{
				if(httpCallBack != null)
					httpCallBack.onFailure(String.valueOf(statusCode), null);
			}
		} catch (ClientProtocolException e) {
			LogUtils.e(e);
			if(httpCallBack != null)
				httpCallBack.onFailure(String.valueOf(statusCode),e);
		} catch (IOException e) {
			LogUtils.e(e);
			if(httpCallBack != null)
				httpCallBack.onFailure(String.valueOf(statusCode),e);
		}catch (Exception e) {
			LogUtils.e(e);
			if(httpCallBack != null)
				httpCallBack.onFailure(String.valueOf(statusCode),e);
		}finally{
			if(httpClient!=null)
				httpClient.getConnectionManager().shutdown();
		}
		
		return null;
		
	}

	@Override
	public void asyncDownloadFile(final String url,final String fileName,final HttpCallBack<File> httpDownloadCallBack) {
		
		asyncThread(new Runnable() {
			@Override
			public void run() {
				syncDownloadFile(url,fileName,httpDownloadCallBack);
			}
		});
	}

	@Override
	public File syncDownloadFile(String url,String fileName) {
		return syncDownloadFile(url, fileName, null);
	}
	
	public File syncDownloadFile(String url,String fileName,HttpCallBack<File> httpDownloadCallBack) {
		
		
		if(TextUtils.isEmpty(url)){
			return null;
		}
		
		File file = null;
		
		BufferedInputStream bis = null;
		
		FileOutputStream fos = null;
		
		HttpClient httpClient = null;
		
		int statusCode = -1;
		try {
			LogUtils.v(url);
			
			if(TextUtils.isEmpty(fileName)){
				return null;
			}
			
			if(httpDownloadCallBack!=null)
				httpDownloadCallBack.onStart(url);
			
			HttpUriRequest httpUriRequest = getHttpUriRequest(url, HttpMethod.GET, null);
			httpClient = getHttpClient();
			HttpResponse httpResponse = httpClient.execute(httpUriRequest);
			statusCode = httpResponse.getStatusLine().getStatusCode();
			if(statusCode == HttpStatus.SC_OK){

				file = new File(fileName);
				fos = new FileOutputStream(file);
				
				long progress = 0;
				
				long count = httpResponse.getEntity().getContentLength();
				
				bis = new BufferedInputStream(httpResponse.getEntity().getContent());
				
				isCancel = false;
				byte[] buffer = new byte[DEFAULT_BYTE_LENGTH];
				int len = 0;
				if(httpDownloadCallBack!=null && count!=-1)
					httpDownloadCallBack.onLoading(progress, count);
				long time = System.currentTimeMillis();
				while((!isCancel) && (len = bis.read(buffer))!=-1){
					fos.write(buffer, 0, len);
					long temp = System.currentTimeMillis();
					if(temp-time>=1000){
						time = temp;
						if(httpDownloadCallBack!=null && count!=-1){
							progress += len;
							httpDownloadCallBack.onLoading(progress, count);
						}
					}
				}
				
				if(httpDownloadCallBack!=null ){
					if(!isCancel){
						progress = count;
						httpDownloadCallBack.onLoading(progress, count);
					}else{
						bis.close();
						fos.close();
						httpDownloadCallBack.onCancel();
						
						if(httpClient!=null)
							httpClient.getConnectionManager().shutdown();
						
						return file;
					}
				}
				
				bis.close();
				fos.close();
				
				if(httpDownloadCallBack!=null && !isCancel)
					httpDownloadCallBack.onSuccess(file);;
				
			}else{
				if(httpDownloadCallBack!=null)
					httpDownloadCallBack.onFailure(String.valueOf(statusCode), null);
			}
			
		} catch (ClientProtocolException e) {
			LogUtils.e(e);
			if(httpDownloadCallBack!=null)
				httpDownloadCallBack.onFailure(String.valueOf(statusCode), e);
			e.printStackTrace();
		} catch (IOException e) {
			LogUtils.e(e);
			if(httpDownloadCallBack!=null)
				httpDownloadCallBack.onFailure(String.valueOf(statusCode), e);
			e.printStackTrace();
		}catch (Throwable e) {
			LogUtils.e(e);
			if(httpDownloadCallBack!=null)
				httpDownloadCallBack.onFailure(String.valueOf(statusCode), e);
		}finally{
			if(httpClient!=null)
				httpClient.getConnectionManager().shutdown();
		}
		
		return file;
	}

	
}

通过HttpURLConnection实现的方式:

/**
 * Http网络通信(HttpURLConnection实现)
 * @author Jenly
 * @date 2014-8-7
 */
public class KingHttp extends AbstractHttp{
	

	public KingHttp() {
		super();
	}

	public KingHttp(int connectionTimeOut, int soTimeOut) {
		super(connectionTimeOut, soTimeOut);
	}

	private HttpURLConnection getHttpURLConnection(String url,HttpMethod httpMethod,Map<String,String> params) throws MalformedURLException, IOException{
		
		String paras = null;
		if(params!=null){
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			for(String key : params.keySet()){
				list.add(new BasicNameValuePair(key, params.get(key)));
			}
			paras = URLEncodedUtils.format(list, DEFAULT_ENCODING);
		}
		
		if(HttpMethod.GET ==httpMethod && paras!=null){
			url += URL_AND_PARA_SEPARATOR;
			url += paras;
		}
		HttpURLConnection httpURLConnection = (HttpURLConnection)new URL(url).openConnection();
		httpURLConnection.setConnectTimeout(connectionTimeOut);
		httpURLConnection.setReadTimeout(soTimeOut);
		httpURLConnection.setUseCaches(false);
		if(HttpMethod.POST ==httpMethod){
			httpURLConnection.setDoOutput(true);
			httpURLConnection.setRequestMethod("POST");
			LogUtils.v("POST:"+ url);
			if(paras!=null)
				httpURLConnection.getOutputStream().write(paras.getBytes());
		}else{
			httpURLConnection.setRequestMethod("GET");
			LogUtils.v("GET:"+ url);
		}
		
		return httpURLConnection;
	}

	@Override
	public void asyncConnect(String url, HttpCallBack<String> httpCallBack) {
		asyncConnect(url, null, httpCallBack);
	}

	@Override
	public void asyncConnect(String url, Map<String, String> params,
			HttpCallBack<String> httpCallBack) {
		asyncConnect(url, params, HttpMethod.GET, httpCallBack);
		
	}

	@Override
	public void asyncConnect(final String url,final Map<String, String> params,
			final HttpMethod httpMethod,final HttpCallBack<String> httpCallBack) {
		asyncThread(new Runnable() {
			@Override
			public void run() {
				syncConnect(url, params, httpMethod, httpCallBack);
			}
		});
		
	}

	@Override
	public String syncConnect(String url) {
		return syncConnect(url, null);
	}

	@Override
	public String syncConnect(String url, Map<String, String> params) {
		return syncConnect(url, params, HttpMethod.GET);
	}

	@Override
	public String syncConnect(String url, Map<String, String> params,
			HttpMethod httpMethod) {
		return syncConnect(url, params, httpMethod, null);
	}

	@Override
	public String syncConnect(String url, Map<String, String> params,
			HttpCallBack<String> httpCallBack) {
		return syncConnect(url, params, HttpMethod.GET, httpCallBack);
	}

	@Override
	public String syncConnect(String url, Map<String, String> params,
			HttpMethod httpMethod, HttpCallBack<String> httpCallBack) {
		
		if(TextUtils.isEmpty(url)){
			return null;
		}
		
		BufferedReader reader = null;
		
		HttpURLConnection httpURLConnection = null;
		
		int statusCode = -1;
		try {
			LogUtils.v(url);
			
			if(httpCallBack!=null){
				httpCallBack.onStart(url);
			}
			httpURLConnection = getHttpURLConnection(url,httpMethod,params);
			httpURLConnection.connect();
			statusCode = httpURLConnection.getResponseCode();
			if(statusCode==HttpURLConnection.HTTP_OK){
				
				reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
				
				StringBuffer buffer = new StringBuffer();
				String line = null;
				
				long progress = 0;
				
				long count = httpURLConnection.getContentLength();
				isCancel = false;
				if(httpCallBack != null && count!=-1)
					httpCallBack.onLoading(progress, count);
				while ((!isCancel) && (line = reader.readLine())!=null) {
					buffer.append(line);
					
					if(httpCallBack != null && count!=-1){
						progress+= line.getBytes().length;
						httpCallBack.onLoading(progress, count);
					}
				}
				
				
				if(httpCallBack != null){
					if(!isCancel){
						progress = count;
						httpCallBack.onLoading(progress, count);
					}else{
						reader.close();
						httpCallBack.onCancel();
						return null;
					}
				}
				reader.close();
				if(httpCallBack != null && !isCancel)
					httpCallBack.onSuccess(buffer.toString());
				
				if(httpURLConnection!=null)
					httpURLConnection.disconnect();
				
				return buffer.toString();
			}else{
				if(httpCallBack != null)
					httpCallBack.onFailure(String.valueOf(statusCode), null);
			}
		} catch (MalformedURLException e) {
			LogUtils.e(e);
			if(httpCallBack != null)
				httpCallBack.onFailure(String.valueOf(statusCode), e);
		} catch (IOException e) {
			LogUtils.e(e);
			if(httpCallBack != null)
				httpCallBack.onFailure(String.valueOf(statusCode), e);
		} catch (Exception e) {
			LogUtils.e(e);
			if(httpCallBack != null)
				httpCallBack.onFailure(String.valueOf(statusCode), e);
		}finally{
			
			if(httpURLConnection!=null){
				httpURLConnection.disconnect();
			}
		}
		
		return null;
	}

	@Override
	public void asyncDownloadFile(final String url,final String fileName,
			final HttpCallBack<File> httpDownloadCallBack) {
		asyncThread(new Runnable() {
			@Override
			public void run() {
				syncDownloadFile(url,fileName,httpDownloadCallBack);
			}
		});
	}

	@Override
	public File syncDownloadFile(String url, String fileName) {
		return syncDownloadFile(url, fileName, null);
	}

	@Override
	public File syncDownloadFile(String url, String fileName,
			HttpCallBack<File> httpDownloadCallBack) {
		
		if(TextUtils.isEmpty(url)){
			return null;
		}
		
		File file = null;
		
		BufferedInputStream bis = null;
		
		FileOutputStream fos = null;
		
		HttpURLConnection httpURLConnection = null;
		
		int statusCode = -1;
		try {
			LogUtils.v(url);
			
			if(TextUtils.isEmpty(fileName)){
				return null;
			}
			
			if(httpDownloadCallBack!=null)
				httpDownloadCallBack.onStart(url);
			
			httpURLConnection = getHttpURLConnection(url,HttpMethod.GET,null);
			httpURLConnection.connect();
			statusCode = httpURLConnection.getResponseCode();
			if(statusCode == HttpURLConnection.HTTP_OK){

				file = new File(fileName);
				fos = new FileOutputStream(file);
				
				long progress = 0;
				
				long count = httpURLConnection.getContentLength();
				
				bis = new BufferedInputStream(httpURLConnection.getInputStream());
				
				isCancel = false;
				byte[] buffer = new byte[DEFAULT_BYTE_LENGTH];
				int len = 0;
				if(httpDownloadCallBack!=null && count!=-1)
					httpDownloadCallBack.onLoading(progress, count);
				long time = System.currentTimeMillis();
				while((!isCancel) && (len = bis.read(buffer))!=-1){
					fos.write(buffer, 0, len);
					long temp = System.currentTimeMillis();
					if(temp-time>=1000){
						time = temp;
						if(httpDownloadCallBack!=null && count!=-1){
							progress += len;
							httpDownloadCallBack.onLoading(progress, count);
						}
					}
				}
				
				if(httpDownloadCallBack!=null ){
					if(!isCancel){
						progress = count;
						httpDownloadCallBack.onLoading(progress, count);
					}else{
						bis.close();
						fos.close();
						httpDownloadCallBack.onCancel();
						
						if(httpURLConnection!=null)
							httpURLConnection.disconnect();
						
						return file;
					}
				}
				
				bis.close();
				fos.close();
				
				if(httpDownloadCallBack!=null && !isCancel)
					httpDownloadCallBack.onSuccess(file);;
				
			}else{
				if(httpDownloadCallBack!=null)
					httpDownloadCallBack.onFailure(String.valueOf(statusCode), null);
			}
			
		} catch (ClientProtocolException e) {
			LogUtils.e(e);
			if(httpDownloadCallBack!=null)
				httpDownloadCallBack.onFailure(String.valueOf(statusCode), e);
		} catch (IOException e) {
			LogUtils.e(e);
			if(httpDownloadCallBack!=null)
				httpDownloadCallBack.onFailure(String.valueOf(statusCode), e);
		}catch (Throwable e) {
			LogUtils.e(e);
			if(httpDownloadCallBack!=null)
				httpDownloadCallBack.onFailure(String.valueOf(statusCode), e);
		}finally{
			if(httpURLConnection!=null)
				httpURLConnection.disconnect();
		}
		
		return file;
	}

}







分享到:
评论

相关推荐

    Android系统应用开发 实验五 网络通信 实验报告

    本实验旨在让学生深入了解Android网络通信技术,并通过实际操作掌握不同类型的网络通信方式,包括利用WebView实现浏览器功能,以及通过HTTP和Socket协议进行数据传输。 首先,实验的第一个任务是开发一个简单的...

    android 实现网络通信

    在Android平台上实现网络通信是移动应用开发中的常见需求,尤其是当需要与硬件设备进行数据交互时,TCP/IP通信协议常被用于建立稳定、可靠的数据传输通道。本篇将深入探讨如何在Android应用中构建TCP客户端,实现...

    Android网络通信实例

    通过学习并实践这个"Android网络通信实例",你将掌握基本的Android网络请求技巧,为进一步深入学习和开发复杂的网络应用打下坚实的基础。记得在实际项目中遵循最佳实践,比如使用HTTPS、处理异常、优化网络请求和...

    Android网络通信的6种实例代码

    下面将详细讨论Android网络通信的6种实例代码及相关知识点。 1. **基础的Java网络API** - `java.net`包提供了基本的网络编程接口,如`Socket`和`ServerSocket`,用于TCP/IP通信,以及`DatagramSocket`和`...

    Android网络通信之NFC

    总的来说,Android网络通信之NFC是一个充满潜力的技术领域,可以实现许多创新应用,如移动支付、数据交换、物联网设备交互等。开发者可以通过深入了解NFC的工作原理和Android API,创建出各种实用且有趣的NFC应用。

    Android网络通信之网络图片上传事例代码

    首先,我们要了解Android网络通信的基本原理。Android系统提供了多种网络通信API,包括HttpURLConnection、HttpClient(已废弃)、Volley、OkHttp等。在现代Android开发中,OkHttp由于其高效、易用和强大的特性,...

    android udp通信示例

    我们将深入理解Android UDP通信的基本原理,并了解如何在Android Studio中实现这一功能。 首先,我们需要了解UDP的基本概念。UDP是一种无连接的协议,它不建立连接,而是直接将数据包发送到网络上,因此它的传输...

    android java 开发can通信demo

    本示例"android java 开发can通信demo"将教你如何在Android应用中实现CAN通信功能。 首先,你需要了解CAN协议的基本概念。CAN协议是一种多主站总线,允许在分布式实时控制系统中进行数据通信。它定义了两种帧格式:...

    Android的三种网络通信方式

    在网络编程中,考虑到Android系统的特性,如网络变化的监听、后台任务的限制等,通常会结合使用上述接口,并配合IntentService、AsyncTask、Handler等机制来实现网络通信。此外,为了提高性能和减少内存消耗,开发者...

    Basic4android网络通信程序

    在IT领域,网络通信是应用程序开发中的核心部分,特别是在移动应用开发中。Basic4android(B4A)是一个强大的Android编程工具,它...对于初学者来说,理解这些基本概念和实践操作,是迈进B4Android网络编程的第一步。

    Android的三种网络通信方式.docx

    java.net.* 接口可以实现基本的网络通信功能,但是在 Android 平台上,这些接口并不是专门为 Android 设计的,因此在某些情况下可能存在一些限制。 2. Org.apache 接口 Org.apache 接口是 Apache 软件基金会提供的...

    Android基于Netty框架实现通信

    在Android开发中,为了实现高效的网络通信,开发者常常会选择使用Netty框架。Netty是一个高性能、异步事件驱动的网络应用程序框架,适用于多种协议的服务器和客户端应用。本篇文章将详细探讨如何在Android环境中利用...

    Android基于局域网socket通信

    Socket是网络通信的一种接口,它提供了进程间通信(IPC)的能力,让两个设备能够通过Internet协议(IP)地址和端口号建立连接。在Android中,我们可以使用Java的内置Socket类来实现这一功能。 要构建一个Android...

    android环境下网络通信

    本文将深入探讨Android网络通信的基本概念、技术栈以及实现过程,旨在帮助开发者理解如何在Android平台上实现实时的手机间聊天功能。 首先,Android网络通信的基础是Java的Socket编程。Socket是TCP/IP协议族的一...

    androidHtttp网络通信测试DEMO

    通过学习这个DEMO,开发者不仅可以掌握基本的Http通信,还能了解到Android网络编程的最佳实践,为构建高效、稳定的应用打下坚实的基础。在实际项目中,可以根据需求选择使用原生的HttpURLConnection、HttpClient,...

    Android实现网络访问

    本篇将详细探讨Android网络访问的基础知识,包括API选择、请求处理、数据解析以及注意事项。 1. **网络访问API选择**:Android提供了多种方式进行网络访问,如HttpURLConnection、HttpClient(已被弃用)、Volley库...

    Android网络通信框架Volley

    **Android网络通信框架Volley详解** Volley是Google在2013年I/O大会上推出的一款专门为Android平台设计的网络通信框架,它以其高效的性能、简单的API和良好的可扩展性,成为了众多Android开发者进行网络请求时的...

    android网络摄像头的各种实现

    Android提供了Socket和HttpClient等基本网络通信工具,但更推荐使用OkHttp或Retrofit这样的现代网络库,它们能提供更好的性能和易用性。 4. **权限管理**:在Android系统中,访问摄像头和网络需要获取相应的权限。...

    基于SIP的Android视频通信终端实现.pdf

    在当前的通信领域中,SIP(Session Initiation Protocol,会话初始协议)和Android平台的结合为开发者提供了实现移动视频通信终端的新思路。SIP是一个应用层的信令协议,主要用于创建、修改和终止一个或多个参与者...

    Android socket通信聊天,客户端+服务端

    在Android平台上进行网络通信时,Socket通信是一种常见且重要的方式,尤其在实现聊天应用时。本文将深入探讨如何在Android中实现基于Socket的客户端和服务端通信,以构建一个简单的聊天应用。 首先,让我们理解...

Global site tag (gtag.js) - Google Analytics