类 : java.net.HttpURLConnection;
1. GET
package com.yarin.android.Examples_08_01;
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.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//以Get方式上传参数
public class Activity03 extends Activity
{
private final String DEBUG_TAG = "Activity03";
/** Called when the activity is first created. */
@SuppressWarnings("unused")
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
TextView mTextView = (TextView)this.findViewById(R.id.TextView_HTTP);
//http地址"?par=abcdefg"是我们上传的参数
String httpUrl = "http://192.168.1.110:8080/httpget.jsp?par=abcdefg";
//获得的数据
String resultData = "";
URL url = null;
try
{
//构造一个URL对象
url = new URL(httpUrl);
}
catch (MalformedURLException e)
{
Log.e(DEBUG_TAG, "MalformedURLException");
}
if (url != null)
{
try
{
// 使用HttpURLConnection打开连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
//得到读取的内容(流)
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
// 为输出创建BufferedReader
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
//使用循环来读取获得的数据
while (((inputLine = buffer.readLine()) != null))
{
//我们在每一行后面加上一个"\n"来换行
resultData += inputLine + "\n";
}
//关闭InputStreamReader
in.close();
//关闭http连接
urlConn.disconnect();
//设置显示取得的内容
if ( resultData != null )
{
mTextView.setText(resultData);
}
else
{
mTextView.setText("读取的内容为NULL");
}
}
catch (IOException e)
{
Log.e(DEBUG_TAG, "IOException");
}
}
else
{
Log.e(DEBUG_TAG, "Url NULL");
}
Button button_Back = (Button) findViewById(R.id.Button_Back);
/* 监听button的事件信息 */
button_Back.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity03.this, Activity01.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
Activity03.this.finish();
}
});
}
}
2.POST
package com.yarin.android.Examples_08_01;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//以post方式上传参数
public class Activity04 extends Activity
{
private final String DEBUG_TAG = "Activity04";
/** Called when the activity is first created. */
@SuppressWarnings("unused")
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
TextView mTextView = (TextView)this.findViewById(R.id.TextView_HTTP);
//http地址"?par=abcdefg"是我们上传的参数
String httpUrl = "http://192.168.1.110:8080/httpget.jsp";
//获得的数据
String resultData = "";
URL url = null;
try
{
//构造一个URL对象
url = new URL(httpUrl);
}
catch (MalformedURLException e)
{
Log.e(DEBUG_TAG, "MalformedURLException");
}
if (url != null)
{
try
{
// 使用HttpURLConnection打开连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
//因为这个是post请求,设立需要设置为true
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
// 设置以POST方式
urlConn.setRequestMethod("POST");
// Post 请求不能使用缓存
urlConn.setUseCaches(false);
urlConn.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
urlConn.connect();
//DataOutputStream流
DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());
//要上传的参数
String content = "par=" + URLEncoder.encode("ABCDEFG", "gb2312");
//将要上传的内容写入流中
out.writeBytes(content);
//刷新、关闭
out.flush();
out.close();
//获取数据
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String inputLine = null;
//使用循环来读取获得的数据
while (((inputLine = reader.readLine()) != null))
{
//我们在每一行后面加上一个"\n"来换行
resultData += inputLine + "\n";
}
reader.close();
//关闭http连接
urlConn.disconnect();
//设置显示取得的内容
if ( resultData != null )
{
mTextView.setText(resultData);
} else
mTextView.setText("读取的内容为NULL");
}
catch (IOException e)
{
Log.e(DEBUG_TAG, "IOException");
}
}
else
{
Log.e(DEBUG_TAG, "Url NULL");
}
Button button_Back = (Button) findViewById(R.id.Button_Back);
/* 监听button的事件信息 */
button_Back.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity04.this, Activity01.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
Activity04.this.finish();
}
});
}
}
HttpURLConnection相比之下,POST比get多了HttpURLConnection对象的设置和参数上传写入OutputStream流.
分享到:
相关推荐
Android HttpUrlConnection使用方法 post and get json.
主要介绍了Android中使用HttpURLConnection实现GET POST JSON数据与下载图片,需要的朋友可以参考下
介绍了Android 中HttpURLConnection与HttpClient使用的简单实例,有需要的朋友可以参考一下
这是一个关于httpurlconnection 的网络post get 图片下载的Demo
里面详细的写了post和get和HttpPost和HttpGet请求
NULL 博文链接:https://zjutsoft.iteye.com/blog/1313754
4. 安卓系统下,AS IDE get post的使用方法,这个演示程序演示了如何用android java去访问网页,获取网页。 访问的是我的csdn主页,见程序中 streamToString getHtml HttpURLConnection conn = (HttpURLConnection) ...
该压缩包中包含3个小程序,第一个是使用HttpURLConnection获取网络资源,包括直接获取、使用get方式获取、使用post方式获取、以及获得网络图片;第二个小程序是使用HttpClient获取网络资源,包括get方式和post方式;...
Android中提供的HttpURLConnection和HttpClient接口可以用来开发HTTP程序。以下是学习中的一些经验。 1、HttpURLConnection接口 首先需要明确的是,Http通信中的POST和GET请求方式的不同。GET可以获得静态页面,...
8.14 远程下载安装Android程序——APKInstaller的应用 8.15 手机下载看3gp影片——Runnable混搭SurfaceView 8.16 访问网站LoginAPI——远程服务器验证程序运行权限 8.17 地震速报!——HttpURLConnection与Service...
Http通信又分为两种方式:get和post,get可以uoqu静态页面,传入参数可以放在url当中,而post方法的传入参数则是放在httprequest(请求)当中。前面提到的HttpUrlConnection接口是java当中的通信接口,而HttpClient...
一些工具类代码块的标准代码: 异步的Get请求 异步的Post请求 Get请求,获得返回数据 向指定 URL 发送POST方法的请求 使用finally块来关闭输出流和输入流
用于异步执行 HttpGet、HttpPost、HttpPut、HttpDelete、Multipart-Put、Multipart-Post 和带有回调的 HttpUrlConnection 的库。 简化网络通信所需的代码量。 兼容 Android 2.2 及以上版本。 特征 兼容Android 2.2 ...
5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少...
5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少...
5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩多少...
5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 手机自动服务纪实 –p254 6.1 您有一条短信popup提醒——常驻BroadcastReceiver的应用 6.2 手机电池计量还剩...
HTTP协议是一款基于短连接的协议,每次交互完毕后连接断开,而HTTP请求又分为GET和POST两种方式,GET请求比较简单,只需要在网址后面用?拼接请求的资源路径,如百度图片输入动漫关键字的地址 ht