一件醉了的事:1、要写访问网络的权限(<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>) 2、必须在分线程里访问
一、get post请求步骤
1、使用URL的openConnection()方法来创建URLConnection对象
URL url=new URL(xxx);
URLConnnection conn=url.openConnection();
2、设置URLConnection的参数和普通请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36");
3、建立连接
xxxxxx
4、访问远程资源,获取信息
in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line=in.readLie())!=null){ result+="\n"+line;}
二、get post请求的书写的区别
1、获得URL时,get方法已包含请求参数(new URL(url+“?”+参数));而post只是单纯的url(new URL(url))
2、建立连接时,get方法直接使用conn.connect()建立连接;而post先设置两个值{conn.setDoOutput(true); conn.setDoInput(true)},然后获取输出流out=new PrintWriter(conn.getOutputstream()),发送请求参数 out.print(参数);设置缓冲 out.flush();
三、URL网络文件读取
1、定义URL对象 URL url=new URL(“http://www....”);
2、打开输入流 Inputstream is=url.openStream();
四、get post请求代码 五、post文件发送
package com.wl.httptest;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.wl.httptest.interfaces.NetworkInsterface;
import android.util.Log;
/**
* 为HttpFactory提供支持,访问网络,返回收到的信息
*
* @author 王琳
*/
public class HttpBase {
// userAgent
private static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
private static URLConnection conn;
private static PrintWriter out;
private static BufferedOutputStream outs;
private static BufferedReader in;
private static String tag = "HttpBase";
/**
* @param strUrl
* 请求地址
* @param params
* 包含请求参数的字符串
* @param method
* 请求的类型
* @return
*/
public static String net(String strUrl, String params, String method) {
String data = null;
// ======【区别1】
if (method.toLowerCase().equals("get")) {
strUrl = strUrl + "?" + params;
}
// ==================
try {
URL url = new URL(strUrl);
conn = url.openConnection();
// 设置参数和普通请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", userAgent);
// ======【区别2】
if (method.toLowerCase().equals("get")) {
conn.connect();
} else {
conn.setDoInput(true);
conn.setDoOutput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(params);
out.flush();// 缓冲
}
// ==================
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
data += line;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
data = "访问异常!" + e.getMessage();
Log.e(tag, "URL异常:" + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
data = "访问异常!" + e.getMessage();
Log.e(tag, "IO异常: " + e.getMessage());
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(tag, "流关闭异常:" + e.getMessage());
}
}
return data;
}
public static String net(String strUrl, String path) {
String data = null;
try {
URL url = new URL(strUrl);
conn = url.openConnection();
// 设置参数和普通请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", userAgent);
conn.setDoInput(true);
conn.setDoOutput(true);
// ==========【post发送文件流】============
outs = new BufferedOutputStream(conn.getOutputStream());
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
byte[] buff = new byte[(int) file.length()];
int len;
while ((len = fis.read(buff)) != -1) {
outs.write(buff, 0, len);
}
// ====================================
outs.flush();// 缓冲
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
data += line;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
data = "访问异常!" + e.getMessage();
Log.e(tag, "URL异常:" + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
data = "访问异常!" + e.getMessage();
Log.e(tag, "IO异常: " + e.getMessage());
} finally {
try {
if (outs != null) {
outs.close();
}
if (in != null) {
in.close();
}
;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(tag, "流关闭异常:" + e.getMessage());
}
}
return data;
}
}
六、get请求的简写
/**
* 根据URL地址获得数据
*/
public void getNetwork(final String url, final NetworkInsterface insterface) {
new Thread() {
public void run() {
InputStream is = null;
try {
URL urlw = new URL(url);
// URLConnection conn = urlw.openConnection();
// is = conn.getInputStream();
is = urlw.openStream();
insterface.OnSuccess(is);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("========", e.getMessage());
insterface.OnFaiure("错了" + e.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("========", e.getMessage());
insterface.OnFaiure("错了" + e.getMessage());
}
};
}.start();
}