网友通过本文主要向大家介绍了Android开发学习之路--网络编程之初体验等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android开发学习之路--网络编程之初体验
一般手机都是需要上网的,一般我们的浏览器就是个webview。这里简单实现下下功能,先编写Android的layout布局:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E-->
<linearlayout 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:orientation="vertical" android:layout_margin="10dp" android:padding="10dp" tools:context="com.example.jared.webviewstudy.MainActivity">
<linearlayout android:layout_width="match_parent" android:layout_height="wrap_content">
<edittext android:id="@+id/netAddress" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content"><button android:id="@+id/openNetAddress" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="0" android:text="Open" android:textallcaps="false">
<webview android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent">
</webview></button></edittext></linearlayout></linearlayout>
这里主要是一个EditText用来输入网址,然后一个Button用来打开网页,webView用来呈现网页。编写代码如下:
package com.example.jared.webviewstudy;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
private EditText networkAddr;
private Button openNetwork;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
networkAddr = (EditText)findViewById(R.id.netAddress);
myWebView = (WebView)findViewById(R.id.webView);
openNetwork = (Button)findViewById(R.id.openNetAddress);
openNetwork.setOnClickListener(new myOnClickListener());
}
class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
String networkAddress = networkAddr.getText().toString();
myWebView.loadUrl("http://"+networkAddress);
}
}
}
还有就是权限问题了:
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
这里通过setWebViewClient方法,实例化一个WebViewClient,loadurl实现网页的加载。运行看下效果:

这里打开了百度和我的博客的地址,界面略难看,勉强看看了。
一般网络编程都是通过http的,下面就来实现下,首先是HttpURLConnection,这个一般是google官方提供的,还有一个HttpClient,本来有的,现在api23也没有了,需要自己加载进来。
先使用HttpURLConnection和HttpClient吧,新建工程,编写layout代码如下:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E-->
<linearlayout 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:orientation="vertical" android:layout_margin="10dp" tools:context="com.example.jared.httpurlconnectionstudy.MainActivity"><button android:id="@+id/sendRequest" android:text="发送请求" android:layout_width="match_parent" android:layout_height="wrap_content">
<scrollview android:layout_width="match_parent" android:layout_height="match_parent">
<textview android:id="@+id/response" android:layout_width="match_parent" android:layout_height="wrap_content">
</textview></scrollview></button></linearlayout>
这里主要就是一个按钮获取数据,然后http请求的数据通过ScrollView可以滑动浏览更多的信息,然后把获取到的信息显示在TextView里面。
编写MainActivity,里面有实现了HttpURLConnection和HttpClient:
package com.example.jared.httpurlconnectionstudy;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final int SHOW_RESPONSE = 1;
private Button sendRequestBtn;
private TextView responseView;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_RESPONSE:
String responseContent = (String)msg.obj;
responseView.setText(responseContent);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
setContentView(R.layout.activity_main);
responseView = (TextView)findViewById(R.id.response);
sendRequestBtn = (Button)findViewById(R.id.sendRequest);
sendRequestBtn.setOnClickListener(new myOnClickListener());
}
private class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sendRequest:
String url = "http://www.baidu.com";
//sendRequestWithHttpURLConnection(url);
sendRequestWithHttpClient(url);
break;
default:
break;
}
}
}
private void sendRequestWithHttpClient(final String url) {
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
Me

