/***Httpget请求**@paramurlPath***/privatestaticStringhttpConnByGet(RequestUrlru,Paramsparams){Stringtoken=(String)params.getMap().get("token");Stringet=(String)params.getMap().get("et");params.getMap().remove("et");if(token!=null){params.getMap().put("token",Md5.md5(token+et+params.getMap().get("uid")+ru.getKeywords()));}StringurlPath=ru.getUrl()+HttpURLConstant.QMARK+HttpURLConstant.COMMON_PARAMS+et+params.toString();//通用参数的设置Log.w("NETURL","geturl:"+urlPath);URLurl=null;HttpURLConnectionurlConnection=null;try{ByteArrayOutputStreamoutStream=newByteArrayOutputStream();byte[]data=newbyte[2048];//应该不会小吧!intlen=0;url=newURL(urlPath);urlConnection=(HttpURLConnection)url.openConnection();//Class<?>clazz=url.openConnection().getClass();//看看返回什么鬼类型//Class<?>clazzSuper=clazz.getSuperclass();//父类又是什么鬼啊!//getInputStream暗中执行了连接,不需要urlConnection.connect();urlConnection.setReadTimeout(readTimeout);//设置请求的超时时间urlConnection.setConnectTimeout(connectTimeout);if(urlConnection.getResponseCode()==HttpURLConnection.HTTP_OK){InputStreaminStream=urlConnection.getInputStream();//获取输入流,此时才真正建立链接while((len=inStream.read(data))!=-1){outStream.write(data,0,len);}inStream.close();if(isDebug){Log.e(TAG,"get返回数据"+newString(outStream.toByteArray()));}returnnewString(outStream.toByteArray());}else{//返回的是HTTP的错误码,不是我们自己定义的错误码!//处理一下http的错误返回码Log.e(TAG,"get网络连接失败"+urlConnection.getResponseCode());return"HTTP_ERROR-"+urlConnection.getResponseCode();}}catch(MalformedURLExceptione){e.printStackTrace();Log.e(TAG,"MalformedURLExceptione");}catch(IOExceptione){e.printStackTrace();Log.e(TAG,"IOExceptione");}finally{if(urlConnection!=null){urlConnection.disconnect();//总是需要关闭的吧!}}returnnull;}/***HttpPost请求**@paramurlpath*@paramparamsDatas*/privatestaticStringhttpConnByPost(RequestUrlru,Paramsparams){Stringtoken=(String)params.getMap().get("token");Stringet=(String)params.getMap().get("et");params.getMap().remove("et");if(token!=null){params.getMap().put("token",Md5.md5(token+et+params.getMap().get("uid")+ru.getKeywords()));}URLurl=null;//根据地址创建URL对象HttpURLConnectionurlConnection=null;//根据URL对象打开链接try{url=newURL(ru.getUrl());//根据地址创建URL对象urlConnection=(HttpURLConnection)url.openConnection();//根据URL对象打开链接urlConnection.setRequestMethod("POST");//设置请求的方式urlConnection.setReadTimeout(readTimeout);//设置请求的超时时间urlConnection.setConnectTimeout(connectTimeout);StringparamsData=HttpURLConstant.COMMON_PARAMS+et+params.toString();//通用参数的使用要注意Log.w("NETURL",url+"?"+paramsData);urlConnection.setRequestProperty("Connection","keep-alive");//设置维持长连接urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");//设置文件的类型urlConnection.setRequestProperty("Content-Length",String.valueOf(paramsData.getBytes().length));//设置数据的长度urlConnection.setRequestProperty("User-Agent","Devond_Watch,Android-device.");//???urlConnection.setDoOutput(true);//发送POST请求必须设置允许输出urlConnection.setDoInput(true);//发送POST请求必须设置允许输入,setDoInput的默认值就是trueurlConnection.setUseCaches(false);//为安全,不允许使用缓存urlConnection.connect();//urlConnection.getInputStream()的时候会自动的打开连接的OutputStreamos=urlConnection.getOutputStream();//获取输出流developeros.write(paramsData.getBytes());os.flush();if(urlConnection.getResponseCode()==HttpURLConnection.HTTP_OK){InputStreamis=urlConnection.getInputStream();//获取响应的输入流对象ByteArrayOutputStreambaos=newByteArrayOutputStream();//创建字节输出流对象intlen=0;//定义读取的长度bytebuffer[]=newbyte[2048];//定义缓冲区while((len=is.read(buffer))!=-1){//按照缓冲区的大小,循环读取baos.write(buffer,0,len);//根据读取的长度写入到os对象中}is.close();//释放资源baos.close();finalStringresult=newString(baos.toByteArray());if(isDebug){Log.e(TAG,"Post返回的数据:"+result);}returnnewString(baos.toByteArray());}else{//返回的是HTTP的错误码,不是我们自己定义的错误码!//处理一下http的错误返回码Log.e(TAG,urlConnection.getResponseCode()+"#Post网络连接失败#"+urlConnection.getErrorStream());return"HTTP_ERROR-"+urlConnection.getResponseCode();}}catch(MalformedURLExceptione){e.printStackTrace();Log.e(TAG,"MalformedURLExceptione");}catch(Exceptione){e.printStackTrace();}finally{if(urlConnection!=null){urlConnection.disconnect();}}Log.e(TAG,"Post请求返回为空");returnnull;}

更多相关文章

  1. android Textview颜色渐变
  2. Android上传文件到Django服务器
  3. Android开发:设置USB tethering 和USB debugging
  4. Android(安卓)全屏无标题详解
  5. Android设置Progress的颜色
  6. android评分条RatingBar自定义设置
  7. android Pull解析xml 使用小结
  8. Android(安卓)开发常用代码片段
  9. springmvc服务端+android客户端的文件上传

随机推荐

  1. Android(安卓)3.0 SDK(R10)及ADT10.0.0正式
  2. Text To Speech 总结
  3. android 手势识别例子
  4. Android(安卓)4.0原生SDK发布,引入新的流
  5. Android(安卓)APK文件在电脑上面运行方法
  6. 从头学Android之Android的数据存储--SQLi
  7. Android深入浅出自定义控件(一)
  8. android AIDL服务
  9. Android之Adapter用法总结
  10. 史上最全的android开源项目框架大全