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

android中简单的HTTP通信

 
阅读更多
anddroid中简单的HTTP通信
privateStringgetHttpResponse(Stringlocation){
Stringresult=null;
URLurl=null;
try{
url=newURL(location);
}catch(MalformedURLExceptione){
Log.e(tag,location+"isnotarihgturl",e);
}
if(url!=null){
HttpURLConnectionurlConn=null;
InputStreamis=null;
OutputStreamos=null;
try{
urlConn=(HttpURLConnection)url.openConnection();
os=urlConn.getOutputStream();
os.write("Hello".getBytes());
is=urlConn.getInputStream();
bytebuffer[]=newbyte[is.available()];
is.read(buffer);
result=newString(buffer);
}catch(IOExceptione){
}finally{
try{
if(is!=null)
is.close();
}catch(IOExceptione){
}
urlConn.disconnect();
}
}
returnresult;
}
注意:需要加入以下权限
<uses-permissionandroid:name="android.permission.INTERNET"></uses-permission>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics