post get 请求 模板
来源:互联网 发布:石家庄数控编程招聘 编辑:程序博客网 时间:2024/05/07 22:55
//线程加载
Runnable runable = new Runnable() {
@Override
public void run() {
String dataByUrl = HttpConnection.request_Detail("你的请求地址URL");
Message message = Message.obtain();
message.obj = dataByUrl;
handler.sendMessage(message);
}
};
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.obj != null) {
String data=msg.obj.toString();
}
};
};
//你自己重新建个包把这个方法放到包下,get,post都可以,通用
/**
*
* httpClent post
*
* @param url
* @return
*/
public static String httpClientPost(String url) {
HttpPost post = new HttpPost(url);
HttpClient mClient = new DefaultHttpClient();
// 数据传输
List<NameValuePair> list = new ArrayList<NameValuePair>();
try {
HttpEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
// 设置传输内容
post.setEntity(entity);
HttpResponse response = mClient.execute(post);
HttpEntity mEntity = response.getEntity();
InputStream stream = mEntity.getContent();
return froatStream(stream);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* 字节流转换
*
* @param mInputStream
* @return
*/
public static String froatStream(InputStream mInputStream) {
// 转换成String
byte[] buffer = new byte[1024];
int len = 0;
StringBuffer sb = new StringBuffer();
try {
while ((len = mInputStream.read(buffer)) != -1) {
sb.append(new String(buffer, 0, len, "UTF-8"));
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* httpclient 请求
*
* @param url
* @return
*/
public static String httpClientGet(String url) {
// 创建一个连接对象
HttpGet hGet = new HttpGet(url);
// 创建一个httpClient 对象
HttpClient mClient = new DefaultHttpClient();
InputStream stream = null;
// 执行请求
try {
HttpResponse mHttpResponse = mClient.execute(hGet);
// 获取返回的数据
HttpEntity entity = mHttpResponse.getEntity();
stream = entity.getContent();
return froatStream(stream);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Runnable runable = new Runnable() {
@Override
public void run() {
String dataByUrl = HttpConnection.request_Detail("你的请求地址URL");
Message message = Message.obtain();
message.obj = dataByUrl;
handler.sendMessage(message);
}
};
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.obj != null) {
String data=msg.obj.toString();
}
};
};
//你自己重新建个包把这个方法放到包下,get,post都可以,通用
/**
*
* httpClent post
*
* @param url
* @return
*/
public static String httpClientPost(String url) {
HttpPost post = new HttpPost(url);
HttpClient mClient = new DefaultHttpClient();
// 数据传输
List<NameValuePair> list = new ArrayList<NameValuePair>();
try {
HttpEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
// 设置传输内容
post.setEntity(entity);
HttpResponse response = mClient.execute(post);
HttpEntity mEntity = response.getEntity();
InputStream stream = mEntity.getContent();
return froatStream(stream);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* 字节流转换
*
* @param mInputStream
* @return
*/
public static String froatStream(InputStream mInputStream) {
// 转换成String
byte[] buffer = new byte[1024];
int len = 0;
StringBuffer sb = new StringBuffer();
try {
while ((len = mInputStream.read(buffer)) != -1) {
sb.append(new String(buffer, 0, len, "UTF-8"));
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* httpclient 请求
*
* @param url
* @return
*/
public static String httpClientGet(String url) {
// 创建一个连接对象
HttpGet hGet = new HttpGet(url);
// 创建一个httpClient 对象
HttpClient mClient = new DefaultHttpClient();
InputStream stream = null;
// 执行请求
try {
HttpResponse mHttpResponse = mClient.execute(hGet);
// 获取返回的数据
HttpEntity entity = mHttpResponse.getEntity();
stream = entity.getContent();
return froatStream(stream);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
0 0
- post get 请求 模板
- GET请求 Post请求
- get请求 post请求
- Get请求 Post请求
- python多线程get与post请求模板代码
- 16.POST、GET请求
- iOS get post请求
- http post get 请求
- IOS Get、Post请求
- get post请求
- ios get post请求
- IOS Get、Post请求
- HTTP GET/POST 请求
- GET/POST请求
- WebService get post请求
- http post get请求
- Android POST GET请求
- POST&&&GET请求比较
- Android fragment 之间传值的方法
- java.io.IOException: 您的主机中的软件中止了一个已建立的连接。
- React Native疑难点,问题深坑最强总结
- shell中条件判断if中的-z到-d的意思
- tomcat根目录下文件
- post get 请求 模板
- 高性能的PHP日志系统 SeasLog
- Android Studio-Gradle项目中添加JNI生成文件(.so文件)
- 按类别统计收费金额 同时组织机构按父子关系自动缩进
- p2p按年化率计算收益简单算法
- C++程序员学习发展方向分析和指导(C++入门学习指导建议必看)
- 已上线的服务窗如何(不影响线上用户)做新功能开发
- “大实若虚”与“大伪似真”
- iClient for JavaScript的几种图层显隐控制