package net.learn2develop.Networking;
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import java.io.InputStreamReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class NetworkingActivity extends Activity {
ImageView img;
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
Log.d("Networking", ex.getLocalizedMessage());
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
Log.d("NetworkingActivity", e1.getLocalizedMessage());
}
return bitmap;
}
/*
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
return DownloadImage(urls[0]);
}
protected void onPostExecute(Bitmap result) {
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(result);
}
}
*/
private class DownloadImageTask extends AsyncTask
<String, Bitmap, Long> {
//---takes in a list of image URLs in String type---
protected Long doInBackground(String... urls) {
long imagesCount = 0;
for (int i = 0; i < urls.length; i++) {
//---download the image---
Bitmap imageDownloaded = DownloadImage(urls[i]);
if (imageDownloaded != null) {
//---increment the image count---
imagesCount++;
try {
//---insert a delay of 3 seconds---
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//---return the image downloaded---
publishProgress(imageDownloaded);
}
}
//---return the total images downloaded count---
return imagesCount;
}
//---display the image downloaded---
protected void onProgressUpdate(Bitmap... bitmap) {
img.setImageBitmap(bitmap[0]);
}
//---when all the images have been downloaded---
protected void onPostExecute(Long imagesDownloaded) {
Toast.makeText(getBaseContext(),
"Total " + imagesDownloaded + " images downloaded" ,
Toast.LENGTH_LONG).show();
}
}
private String DownloadText(String URL)
{
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
} catch (IOException e) {
Log.d("NetworkingActivity", e.getLocalizedMessage());
return "";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer))>0) {
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {
Log.d("NetworkingActivity", e.getLocalizedMessage());
return "";
}
return str;
}
private class DownloadTextTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
return DownloadText(urls[0]);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
}
}
private String WordDefinition(String word) {
InputStream in = null;
String strDefinition = "";
try {
in = OpenHttpConnection(
"http://services.aonaware.com/DictService/DictService.asmx/Define?word=" + word);
Document doc = null;
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
doc = db.parse(in);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doc.getDocumentElement().normalize();
//---retrieve all the <Definition> elements---
NodeList definitionElements =
doc.getElementsByTagName("Definition");
//---iterate through each <Definition> elements---
for (int i = 0; i < definitionElements.getLength(); i++) {
Node itemNode = definitionElements.item(i);
if (itemNode.getNodeType() == Node.ELEMENT_NODE)
{
//---convert the Definition node into an Element---
Element definitionElement = (Element) itemNode;
//---get all the <WordDefinition> elements under
// the <Definition> element---
NodeList wordDefinitionElements =
(definitionElement).getElementsByTagName(
"WordDefinition");
strDefinition = "";
//---iterate through each <WordDefinition> elements---
for (int j = 0; j < wordDefinitionElements.getLength(); j++) {
//---convert a <WordDefinition> node into an Element---
Element wordDefinitionElement =
(Element) wordDefinitionElements.item(j);
//---get all the child nodes
没有合适的资源?快使用搜索试试~ 我知道了~
Android4高级编程的源代码
共1215个文件
class:367个
png:294个
xml:141个
需积分: 9 12 下载量 166 浏览量
2014-12-12
22:40:19
上传
评论
收藏 4.58MB ZIP 举报
温馨提示
Android 4高级编程 第3版 》由Android权威专家编写 涵盖了所有最新的内容 是学习使用Android 4 SDK开发移动应用程序的理想指南
资源推荐
资源详情
资源评论
收起资源包目录
Android4高级编程的源代码 (1215个子文件)
resources.ap_ 184KB
resources.ap_ 184KB
resources.ap_ 184KB
resources.ap_ 16KB
resources.ap_ 12KB
resources.ap_ 12KB
resources.ap_ 12KB
resources.ap_ 12KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 11KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
resources.ap_ 10KB
ImageSwitcher.apk 189KB
Gallery.apk 189KB
Grid.apk 189KB
Layouts.apk 19KB
LBS.apk 18KB
ContentProviders.apk 17KB
Networking.apk 17KB
Services.apk 17KB
Databases.apk 16KB
SMS.apk 16KB
Sockets.apk 16KB
Fragments.apk 16KB
UsingPreferences.apk 16KB
Dialog.apk 16KB
UsingIntent.apk 16KB
Intents.apk 15KB
Files.apk 15KB
BasicViews1.apk 15KB
PassingData.apk 15KB
MyActionBar.apk 15KB
Threading.apk 15KB
BasicViews4.apk 15KB
Notifications.apk 15KB
ListFragmentExample.apk 15KB
JSON.apk 15KB
BasicViews6.apk 15KB
BasicViews5.apk 15KB
UIActivity.apk 15KB
LocationTracker.apk 15KB
PreferenceFragmentExample.apk 14KB
Provider.apk 14KB
Menus.apk 14KB
DialogFragmentExample.apk 14KB
BasicViews2.apk 14KB
Orientations.apk 14KB
WebView.apk 14KB
Emails.apk 14KB
BasicViews3.apk 14KB
Activity101.apk 13KB
UICode.apk 13KB
HelloWorld.apk 13KB
Clock.apk 13KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
proguard.cfg 1KB
共 1215 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
davidy2000
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功