Android中怎么访问Internet

今天就跟大家聊聊有关Android中怎么访问Internet,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Android应用访问Internet,首先需要赋予应用有访问Internet的权限:在AndroidManifest.xml中添加:

<uses-permission android:name=”android.permission.INTERNET” />

然后实现DrawMap()如下:

private void drawMap(){  try{      graphics2D.clear(Color.WHITE);  graphics2D.Reset();  for(int x=6;x<8;x++)  {  for(int y=3;y<5;y++){  String urlString="http://www.mapdigit.com/guidebeemap";  urlString+="/maptile.php?type=MICROSOFTMAP";  urlString+="&x="+x+"&y="+y+"&z=14";  URL url=new URL(urlString);  URLConnection connection=url.openConnection();  HttpURLConnection httpConnection=(HttpURLConnection)connection;  int responseCode=httpConnection.getResponseCode();  if(responseCode==HttpURLConnection.HTTP_OK){  InputStream stream=httpConnection.getInputStream();  Bitmap bitmap=BitmapFactory.decodeStream(stream);  int []buffer=new int[bitmap.getHeight()  * bitmap.getWidth()];  bitmap.getPixels(buffer, 0, bitmap.getWidth(), 0, 0,  bitmap.getWidth(), bitmap.getHeight());  graphics2D.drawImage(buffer,bitmap.getWidth(),  bitmap.getHeight(),(x-6)*256,(y-3)*256);      }  }  }  graphic2dView.refreshCanvas();    }catch(Exception e){      } }

Android中访问Internet类主要定义在java.net.* 和android.net.*包中。上面显示结果如下:

Android中怎么访问Internet

看完上述内容,你们对Android中怎么访问Internet有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。