Android apache httpClient

判断网络状态:

public static boolean isConnection(Context context) {
<span style="white-space:pre">	</span>ConnectivityManager cm = 
<span style="white-space:pre">			</span>(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
<span style="white-space:pre">	</span>if(cm == null) {
<span style="white-space:pre">		</span>return false;
<span style="white-space:pre">	</span>} else {
<span style="white-space:pre">		</span>NetworkInfo[] info = cm.getAllNetworkInfo();   
        if (info != null) {
            for (int i = 0; i < info.length; i++) {   
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {   
                    return true;   
                }
            }
        }
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return false;
}

连接后台:(登录为例子)

/**

* @param urlPath http://localhost:8080/myWebSocket/login
* @param state 1,2,3
* @param name
* @param pw
* @param encode "utf-8"
* @return
* @throws IOException 
* @throws ClientProtocolException 
*/

public static String login(String urlPath,int state, String name,String pw,String encode) {
	Log.i("main", "input");
	
	List<NameValuePair> list = 
			new ArrayList<NameValuePair>();
	BasicNameValuePair statePair = 
			new BasicNameValuePair("state", String.valueOf(state));
	BasicNameValuePair namePair = 
			new BasicNameValuePair("loginName", name);
	BasicNameValuePair pwPair = 
			new BasicNameValuePair("loginPw", pw);
	list.add(statePair);
	list.add(namePair);
	list.add(pwPair);
	try {
		UrlEncodedFormEntity enity = 
				new UrlEncodedFormEntity(list,encode);
		HttpPost httpPost = new HttpPost(urlPath);
		httpPost.setEntity(enity);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpResponse httpResponse = httpClient.execute(httpPost);
		
		if (httpResponse.getStatusLine().getStatusCode() == 200) {
			//获取响应
			InputStream inputStream = httpResponse.getEntity().getContent();
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			byte[] data = new byte[1024];
			int len = 0;
			String result = "";
			try {
				if(inputStream != null) {
					while( (len = inputStream.read(data)) != -1 ){
						outputStream.write(data, 0, len);
					}
					result = new String(outputStream.toByteArray(), encode);
					Log.i("main","result"+result);
					return result;
				}
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
				Log.e("login http is error", "read http error");
			} finally {
				inputStream.close();
				outputStream.close();
			}
			
		}
			 
	} 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 null;
}

后台只需要写一个一般的登录web后台就可以,这里我使用的是servelet。不管web后台使用什么样的框架搭建只需要urlPath正确即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值