首先是Get:

布局非常easy。就一个button,主要看一下MainActivity吧:

package com.francis.httpget;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; public class MyActivity extends Activity { BufferedReader bufferedReader;
InputStreamReader inputStreamReader;
InputStream inputStream; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<String,Void,Void>() {
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL(params[0]);
URLConnection connection = url.openConnection(); inputStream = connection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
bufferedReader = new BufferedReader(inputStreamReader); String line;
while (true) {
if ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} return null;
}
}.execute("http://fanyi.youdao.com/openapi.do?keyfrom=HttpGetFrancis&key=893755706&type=data&doctype=xml&version=1.1&q=这里是有道翻译API");
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

url为有道翻译提供的API,很方便。控制台输出如图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZnJhbmNpc3NoaQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

接着是Post:

非常easy,这里仅仅给出提交post请求的Button的点击事件:

和Get相比有2点须要注意:

1.url与參数是分开的;

2.connection.setDoInput(true);

   connection.setDoOutput(true);

   connection.setRequestMethod("POST");

// Post
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<String,Void,Void>() {
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST"); outputStream = connection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write("keyfrom=HttpGetFrancis&key=893755706&type=data&doctype=xml&version=1.1&q=这里是有道翻译API");
bufferedWriter.flush(); inputStream = connection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream,"UTF-8");
bufferedReader = new BufferedReader(inputStreamReader); String line;
while (true) {
if ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} return null;
}
}.execute("http://fanyi.youdao.com/openapi.do");
}
});

Android利用Get、Post 获取网络数据的更多相关文章

  1. ListView获取网络数据并展示优化练习

    权限: <uses-permission android:name="android.permission.INTERNET"></uses-permission ...

  2. Android中获取网络数据时的分页加载

    //此实在Fragment中实现的,黄色部分为自动加载,红色部分是需要注意的和手动加载,    蓝色部分是睡眠时间,自我感觉不用写  ,还有就是手动加载时,不知道为什么进去后显示的就是最后一行,求大神 ...

  3. android—获取网络数据

    取网络数据主要靠发交易(或者说请求,接口等),而这些交易由java中的网络通信,HttpURLConnection和HttpClient实现,以下是具体例子.   大家都知道,网络通信,发送请求有两种 ...

  4. Android Volley和Gson实现网络数据加载

    Android Volley和Gson实现网络数据加载 先看接口 1 升级接口 http://s.meibeike.com/mcloud/ota/cloudService POST请求 参数列表如下 ...

  5. android客户端从服务器端获取json数据并解析的实现代码

    今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下   首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的U ...

  6. (转)android客户端从服务器端获取json数据并解析的实现代码

    今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下       首先客户端从服务器端获取json数据 1.利用HttpUrlConnection   复制代码 ...

  7. android客户端从服务器端获取json数据并解析的实现代码(重要)

    首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exc ...

  8. [置顶] 获取网络数据中的数组显示成ListView的简单流程

    首先说一下  这是我自己的个人笔记,如果想看看,不用看细节,可以看流程. 定义一个线程池 ExecutorService pool = Executors.newFixedThreadPool(15) ...

  9. Swift实战-豆瓣电台(三)获取网络数据

    观看地址:http://v.youku.com/v_show/id_XNzMwMzQxMzky.html 这节内容,我们先说了怎么将storyboard中的组件在类中进行绑定.然后写了一个类用来获取网 ...

  10. 使用NSURLSession获取网络数据和下载文件

    使用NSURLSession获取网络数据 使用NSURLSession下载文件

随机推荐

  1. CCNP交换实验(7) -- NAT

    1.静态NAT2.动态NAT3.复用内部全局地址的NAT(PAT) enableconf tno ip do loenable pass ciscoline con 0logg syncexec-t ...

  2. 解决cocoapods在64位iOS7系统以下的警告问题

    今天碰到一个非常奇怪的问题.XCODE提示这种警告 Pods was rejected as an implicit dependency for 'libPods.a' because its ar ...

  3. web端、android端的文件上传

    1.web端的文件上传. 这里是利用了第三方的jar包.这里所需要的jar包我已经上传到本博客的资源里了,以下是连接 http://download.csdn.net/detail/caihongsh ...

  4. SuperSocket应用之FTP源码解析

    一 简述 命令行协议是一种使用比较多的协议,其优点在于使用简单易于扩展性,同时也利于解析和使用.FTP,POP,SMTP等均采用命令行协议,其中FTP在早起互联网时期成为网络资源共享的主要方式,可见F ...

  5. const限定符的作用

    const限定符的作用:                                     1.定义const常量:const可以将一个对象变成一个常量,不可被修改,所以定义的 时候必须进行初始 ...

  6. 基础知识(3)- Java的基本程序设计结构

    3.1 一个简单的Java应用程序 3.2 注释 3.3 数据类型  3.3.1 整型  3.3.2 浮点类型  3.3.3 char类型  3.3.4 boolean类型 3.4 变量  3.4.1 ...

  7. 2013Esri全球用户大会之ArcGIS for Desktop

    Q1:ArcGIS 10.2 for Desktop中有哪些新特性?     增强的质量和性能        扩展并行处理能力        许多软件质量的改进        优化的文件处理     ...

  8. WPF入门介绍

    Windows Vista已经于2007年1月30正式发行零售版本,安装Vista的计算机将会大量出现.在Vista时代,身为编程员,就一定要具备Vista桌面应用开发的能力.而开发Vista桌面应用 ...

  9. SQLite/嵌入式数据库

    SQLite/嵌入式数据库 的项目要么不使用数据库(一两个文配置文件就可以搞定),要么就会有很多的数据,用到 postgresql,操练sqlite的还没有.现在我有个自己的小测试例子,写个数据库对比 ...

  10. Codeforces325-B(二分搜索)

    题目:B. Stadium and Games 分析:问题可以转化为下面的等式求解问题: 由于n在10^18范围内,所以k的范围是从0到63即可,这样就可以枚举k,二分m,然后所有符合条件的就是答案了 ...