网络

1.向服务器发送http请求

public boolean sendPhoneInfo(String path){
URL url = null;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
return true;
}
} catch (Exception e) {
e.printStackTrace();
}

return false;
}
new Thread(){
@Override
public void run(){
App.isSuccess = sendPhoneInfo(path);
Log.d("czDebug",path);
}
}.start();

2.获取IP地址
public static String getLocalIpAddress(){
try
{
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
{
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()&& InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()))
{
return inetAddress.getHostAddress().toString();
}
}
}
}
catch (SocketException ex)
{
Log.e("WifiPreferenceIpAddress", ex.toString());
}
return null;
}
*依赖 httpclient-4.5.2.jar

3.WebView

源码

public class WebViewActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);

//对象
WebView webVeiw =(WebView)findViewById(R.id.webview);
String url = "http://10.237.32.10";
//设置
webVeiw.setWebViewClient(new WebViewClient(){//本activity中显示
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
WebSettings settings = webVeiw.getSettings();
settings.setJavaScriptEnabled(true);//启用js
//载入
webVeiw.loadUrl(url);
}
}

布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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-permission android:name="android.permission.INTERNET"></uses-permission>
 
posted @ 2016-09-23 17:28  cunzai201206  阅读(118)  评论(0编辑  收藏  举报