浏览 3135 次
锁定老帖子 主题:Android 网络存储的例子
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-03-16
这一次,讲解Android 网络存储相关的知识。 1)新建项目,项目名称为:NetworkDemo 2)新建类,类名为:NetworkActivity1,继承Activity,代码如下所示: package com.mesada.demo; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.KeyEvent; import android.widget.EditText; public class NetworkActivity1 extends Activity { private EditText mEditText; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mEditText = (EditText) findViewById(R.id.EditText01); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) { Uri uri = Uri.parse("mailto:343827585@qq.com"); // 发送邮件的地址 Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "网络存储"); intent.putExtra(android.content.Intent.EXTRA_TEXT, String.valueOf(mEditText.getText())); startActivity(intent); this.finish(); return true; } return super.onKeyDown(keyCode, event); } } 3)以上类是保存内容到网络上的,要从网络上获得数据,我们新建另外一个类,也继承于Activity,代码如下所示: package com.mesada.demo; import java.io.BufferedInputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import org.apache.http.util.ByteArrayBuffer; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class NetworkActivity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.getdata); TextView tv=(TextView)findViewById(R.id.TextView01); String msg=""; try { URL url=new URL("http://linux.chinaitlab.com/command/723482.html"); URLConnection con=url.openConnection(); InputStream is=con.getInputStream(); BufferedInputStream bis=new BufferedInputStream(is); ByteArrayBuffer baf=new ByteArrayBuffer(100); int current=0; while ((current=bis.read())!= -1) { baf.append((byte)current); } msg=new String(baf.toByteArray(),"GBK"); } catch (Exception e) { msg=e.getMessage(); } tv.setText(msg); } } 3) main.xml 文件如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/widget34" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <EditText android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> </LinearLayout> getdata.xml文件如下所示: <?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </ScrollView> </LinearLayout> 4)AndroidMainfest.xml 文件如下所示: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mesada.demo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".NetworkActivity1" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NetworkActivity2"> </activity> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest> 5)完成。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |