实现最简单的网络demo
2011-11-14 17:12
337 查看
网络编程的第一个基点:获取最简答的数据
HttpDemoMain.java 主体代码
package com.yht.test.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HttpDemoMain extends Activity implements android.view.View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startUpHttp = (Button) findViewById(R.id.btn);
startUpHttp.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
URL url = new URL("http://www.baidu.com");
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
String resultData = null;
while((inputLine = buffer.readLine()) != null){
resultData += (inputLine + "\n");
}
System.out.println(resultData);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="start http"
/>
</LinearLayout>
AndroidManifest.xml 添加网络权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yht.test.http"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HttpDemoMain"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
HttpDemoMain.java 主体代码
package com.yht.test.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HttpDemoMain extends Activity implements android.view.View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startUpHttp = (Button) findViewById(R.id.btn);
startUpHttp.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
URL url = new URL("http://www.baidu.com");
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
String resultData = null;
while((inputLine = buffer.readLine()) != null){
resultData += (inputLine + "\n");
}
System.out.println(resultData);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="start http"
/>
</LinearLayout>
AndroidManifest.xml 添加网络权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yht.test.http"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HttpDemoMain"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
相关文章推荐
- 关于使用Java实现的简单网络爬虫Demo
- 关于使用Java实现的简单网络爬虫Demo
- 关于使用Java实现的简单网络爬虫Demo
- Android 框架设计Demo,一个简单的MVP示例搜索功能,网络请求用Retrofit+RxJava实现
- SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序
- 关于使用Java实现的简单网络爬虫Demo
- 高并发MINA框架,网络编程(SOCKET)实现,简单的网络聊天DEMO
- 我有个简单网络传输加密的想法,但不会实现,请高手给出代码
- Android中AsyncTask的简单用法及实现网络图片下载的实例
- android listview 的弹性滑动 简单demo 实现
- 视频播放网站CDN内容分发网络简单代码实现
- WebClient.DownloadFileAsync实现下载网络数据-简单的下载软件
- ESFramework Demo -- 简单的网络硬盘Demo(附源码)
- 概率神经网络 (PNN) 应用的简单DEMO
- 通信基础篇小项目-----简单网络画板的的实现
- Python实现简单的网络爬虫
- Reactor模式的.net版本简单实现--DEMO
- [tensorflow应用之路]什么是深度神经网络——通过实现简单的神经网络理解DNN
- 用JS实现简单的神经网络算法
- SpringMVC+mybatis+Spring框架整合+简单实现的demo