android http连接服务器
来源:互联网 发布:程序员娶个护士 编辑:程序博客网 时间:2024/05/15 23:50
判断网络连接情况
public static boolean isConnected(Context context) {// <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">ConnectivityManager主要管理和网络连接相关的操作</span><span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;"> </span>ConnectivityManager conManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);// 网络信息 <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">NetworkInfo类包含了对wifi和mobile两种网络模式连接的详细描述</span>NetworkInfo networkInfo = conManager.getActiveNetworkInfo();// 检查网络if (networkInfo != null) {return conManager.getActiveNetworkInfo().isConnected();}return false;}
httppost通过post机制将提价的表单放在html header中用户在URL中看不到
post将数据放在List<<span style="font-family: Arial, Helvetica, sans-serif;">NameValuePair</span>>中
<pre name="code" class="java">/** * HTTP Post 通信处理 * @param context * @param apiUrl * @param nameValuePair * @return String * @throws Exception */public static String httpPost(Context context, String apiUrl, List<NameValuePair> nameValuePair, String baseUserAgent) throws Exception {//HttpClientHttpClient httpClient = new DefaultHttpClient();HttpParams httpParameters = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParameters, 10000);HttpConnectionParams.setSoTimeout(httpParameters, 10000);try {baseUserAgent = baseUserAgent + AppInfoUtil.getAppVersionName(context);} catch(Exception e) {return "";}baseUserAgent = baseUserAgent + " " + httpClient.getParams().getParameter("http.useragent");//(http.useragent)httpClient.getParams().setParameter("http.useragent", baseUserAgent);//HTTP PostHttpPost httppost = new HttpPost(apiUrl);int responseCode = 0;String result = "";httppost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));//HTTP post通信HttpResponse response = httpClient.execute(httppost);//返回值responseCode = response.getStatusLine().getStatusCode();if(responseCode == HttpStatus.SC_OK) {ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();response.getEntity().writeTo(byteArrayOutputStream);result = byteArrayOutputStream.toString();} else if(responseCode == HttpStatus.SC_NOT_FOUND) {//服务器连接失败throw new FileNotFoundException();} else if(responseCode == HttpStatus.SC_REQUEST_TIMEOUT) {//连接超时throw new Exception();}return result;}get将表单拼接到url中,用户是可以看到的
get将数据拼到url后面 eg http://172.23.1.23//api01/?username=""&pwd=""<pre name="code" class="java">/** * HTTP GET通信 * @param url * @return * @throws Exception */public static String httpGet2(Context context, String url, String userAgent) throws Exception {// offline// 网络连接确认if (NetworkUtil.isConnected(context) == false) {throw new Exception();}//HttpParams httpParameters = new BasicHttpParams();// HttpConnectionParams.setConnectionTimeout(httpParameters, 40000);// HttpConnectionParams.setSoTimeout(httpParameters, 40000);// HttpClient httpClient = new DefaultHttpClient(httpParameters);App app = (App)((Activity)context).getApplication();HttpClient httpClient = app.getHttpClient();////HttpParams params = httpClient.getParams();////HttpConnectionParams.setConnectionTimeout(params, 10000);//HttpConnectionParams.setSoTimeout(params, 10000);HttpGet request = new HttpGet(url);request.addHeader(Constant.USER_AGENT, userAgent);HttpResponse httpResponse = null;try {httpResponse = httpClient.execute(request);// 通信失败if (httpResponse == null) {throw new Exception();}}catch (Exception e) {TCILog.e("myTask", "HTTP error === " + e.getMessage());throw new Exception();}String response = null;if (httpResponse != null&& httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {HttpEntity httpEntity = httpResponse.getEntity();try {response = EntityUtils.toString(httpEntity);} catch (Exception e) {TCILog.e("myTask", "EntityUtils.toString error === " + e.getMessage());throw new Exception();} finally {try {httpEntity.consumeContent();} catch (IOException e) {TCILog.e("myTask", "httpEntity.consumeContent error === " + e.getMessage());throw new Exception();}}} else {HttpEntity httpEntity = httpResponse.getEntity();try {response = EntityUtils.toString(httpEntity);} catch (Exception e) {TCILog.e("myTask", "EntityUtils.toString === " + e.getMessage());throw new Exception();}throw new Exception();}return response;}
0 0
- android http连接服务器
- android HTTP形式连接服务器
- Android开发 Http连接服务器
- android使用http协议连接服务器
- android HttP请求服务器,连接超时
- Androidの网络Http之判断是否连接服务器
- Android之网络Http之判断是否连接服务器
- Android连接服务器请求架包之 Async-http-client
- Android的网络Http判断是否连接服务器
- 短连接http服务器陷阱
- Qt---QTcpSocket连接http服务器
- 服务器HTTP长连接与短连接
- android json http连接
- android http 连接通信
- android http 连接
- android连接http总结
- android http 连接
- android http连接设置
- 从今天起,记录每一个工作日(10-30)
- Valid Parentheses
- 开源的推荐系统简介TOP 10
- Installing and Configuring ModSecurity
- 【模电】焊接
- android http连接服务器
- 关于lw_oopc
- mfc 双缓冲
- Android 笔记 -- android系统action大全
- Android数据库高手秘籍(八)——使用LitePal的聚合函数
- 10g数据库表空间出现负数!!
- git(分布式版本控制系统)
- 解决Win8不能共享文件夹的问题
- 微网站,微信网站