您的位置:首页 > 理论基础 > 计算机网络

网络

2016-09-23 17:28 295 查看
1.向服务器发送http请求
一
publicbooleansendPhoneInfo(Stringpath){
URLurl=null;
try{
url=newURL(path);
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
returntrue;
}
}catch(Exceptione){
e.printStackTrace();
}

returnfalse;
}

newThread(){
@Override
publicvoidrun(){
App.isSuccess=sendPhoneInfo(path);
Log.d("czDebug",path);
}
}.start();

2.获取IP地址

publicstaticStringgetLocalIpAddress(){
try
{
for(Enumeration<NetworkInterface>en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();)
{
NetworkInterfaceintf=en.nextElement();
for(Enumeration<InetAddress>enumIpAddr=intf.getInetAddresses();enumIpAddr.hasMoreElements();)
{
InetAddressinetAddress=enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress()&&InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()))
{
returninetAddress.getHostAddress().toString();
}
}
}
}
catch(SocketExceptionex)
{
Log.e("WifiPreferenceIpAddress",ex.toString());
}
returnnull;
}
*依赖httpclient-4.5.2.jar

3.WebView

源码

publicclassWebViewActivityextendsAppCompatActivity{

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);

//对象
WebViewwebVeiw=(WebView)findViewById(R.id.webview);
Stringurl="http://10.237.32.10";
//设置
webVeiw.setWebViewClient(newWebViewClient(){//本activity中显示
@Override
publicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){
view.loadUrl(url);
returntrue;
}
});
WebSettingssettings=webVeiw.getSettings();
settings.setJavaScriptEnabled(true);//启用js
//载入
webVeiw.loadUrl(url);
}
}


布局文件

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cz.czapp.MainActivity">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView">
</ListView>
</RelativeLayout>

权限

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



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: