【工具类】安卓开发 HttpPost和HttpGet请求
2015-10-27 19:07
465 查看
客户端和服务器端交流需要使用网络连接,考虑到减少重复代码,于是整理了一个网络连接工具类
最后两个参数分别是操作成功和失败的回调
package com.example.panda.easyexpress10;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by Panda on 2015/9/8.
*/
public class DoNet
{
private int methodId;
private String urlStr;
private String paramsString;
private String resultStr;
public DoNet(final int methodId, final String urlStr, final String paramsString, final OkCallBack ok,final NotOkCallBack notok) {
System.out.println("------------------------------执行构造函数");
this.methodId=methodId;
this.urlStr=urlStr;
this.paramsString = paramsString;
this.resultStr = "";
new AsyncTask<Void,Void,String>(){
@Override
protected void onPostExecute(String s) {
if (s!=null){
if (ok!=null){
System.out.println("------------------------------提示:已成功取回数据!" +s);
ok.OnOk(s);
}
}else{
if (notok!=null){
System.out.println("------------------------------请求失败,请检查Url是否正确!" );
notok.OnNotOk();
}
}
super.onPostExecute(s);
}
@Override
protected String doInBackground(Void... params) {
System.out.println("------------------------------正在访问:"+urlStr);
StringBuilder sb=new StringBuilder();
try {
URLConnection urlc;
if (methodId==1){//1代 表get
System.out.println("------------------------------执行get方法,访问:"+urlStr+"?"+paramsString);
urlc=new URL(urlStr+"?"+paramsString).openConnection();
}else{
System.out.println("------------------------------执行post方法,访问:"+urlStr+"参数"+paramsString);
urlc= new URL(urlStr).openConnection();
urlc.setDoOutput(true);
urlc.setDoInput(true);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(urlc.getOutputStream(),"utf-8"));
bw.write(paramsString);
bw.flush();//必须要!!!!!!
}
System.out.println("------------------------------即将读取数据.." );
//开始读取数据
BufferedReader br=new BufferedReader(new InputStreamReader(urlc.getInputStream(),"utf-8"));
String line;
int count=0;
while((line=br.readLine())!=null){
sb.append(line);
System.out.println("------------------------------正在读取第" + (count++) +"行");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}.execute();
}
public interface OkCallBack{
Void OnOk(String result);
}
public interface NotOkCallBack{
Void OnNotOk();
}
}
在点击事件中调用
public void onClick(View v) {
new DoNet(
2, //2代表 post
Varbs.HostSting+"check/",
"uid="+Varbs.uid,//参数请求格式 "uid=5"
new DoNet.OkCallBack() {
public Void OnOk(String result) {
if (result.trim().equals("T")) {
Toast.makeText(getApplicationContext(), "签到成功", Toast.LENGTH_SHORT).show();
}else if(result.trim().equals("AlredyChecked")) {
Toast.makeText(getApplicationContext(), "明天再签到吧!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "签到失败", Toast.LENGTH_SHORT).show();
}
return null;
}
},
new DoNet.NotOkCallBack() {
public Void OnNotOk() {
System.out.println("请检查网络连接!");
Toast.makeText(getApplicationContext(), "请检查网络连接", Toast.LENGTH_SHORT).show();
return null;
}
});
}
最后两个参数分别是操作成功和失败的回调
package com.example.panda.easyexpress10;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by Panda on 2015/9/8.
*/
public class DoNet
{
private int methodId;
private String urlStr;
private String paramsString;
private String resultStr;
public DoNet(final int methodId, final String urlStr, final String paramsString, final OkCallBack ok,final NotOkCallBack notok) {
System.out.println("------------------------------执行构造函数");
this.methodId=methodId;
this.urlStr=urlStr;
this.paramsString = paramsString;
this.resultStr = "";
new AsyncTask<Void,Void,String>(){
@Override
protected void onPostExecute(String s) {
if (s!=null){
if (ok!=null){
System.out.println("------------------------------提示:已成功取回数据!" +s);
ok.OnOk(s);
}
}else{
if (notok!=null){
System.out.println("------------------------------请求失败,请检查Url是否正确!" );
notok.OnNotOk();
}
}
super.onPostExecute(s);
}
@Override
protected String doInBackground(Void... params) {
System.out.println("------------------------------正在访问:"+urlStr);
StringBuilder sb=new StringBuilder();
try {
URLConnection urlc;
if (methodId==1){//1代 表get
System.out.println("------------------------------执行get方法,访问:"+urlStr+"?"+paramsString);
urlc=new URL(urlStr+"?"+paramsString).openConnection();
}else{
System.out.println("------------------------------执行post方法,访问:"+urlStr+"参数"+paramsString);
urlc= new URL(urlStr).openConnection();
urlc.setDoOutput(true);
urlc.setDoInput(true);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(urlc.getOutputStream(),"utf-8"));
bw.write(paramsString);
bw.flush();//必须要!!!!!!
}
System.out.println("------------------------------即将读取数据.." );
//开始读取数据
BufferedReader br=new BufferedReader(new InputStreamReader(urlc.getInputStream(),"utf-8"));
String line;
int count=0;
while((line=br.readLine())!=null){
sb.append(line);
System.out.println("------------------------------正在读取第" + (count++) +"行");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}.execute();
}
public interface OkCallBack{
Void OnOk(String result);
}
public interface NotOkCallBack{
Void OnNotOk();
}
}
在点击事件中调用
public void onClick(View v) {
new DoNet(
2, //2代表 post
Varbs.HostSting+"check/",
"uid="+Varbs.uid,//参数请求格式 "uid=5"
new DoNet.OkCallBack() {
public Void OnOk(String result) {
if (result.trim().equals("T")) {
Toast.makeText(getApplicationContext(), "签到成功", Toast.LENGTH_SHORT).show();
}else if(result.trim().equals("AlredyChecked")) {
Toast.makeText(getApplicationContext(), "明天再签到吧!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "签到失败", Toast.LENGTH_SHORT).show();
}
return null;
}
},
new DoNet.NotOkCallBack() {
public Void OnNotOk() {
System.out.println("请检查网络连接!");
Toast.makeText(getApplicationContext(), "请检查网络连接", Toast.LENGTH_SHORT).show();
return null;
}
});
}
相关文章推荐
- HttpClient使用详解
- 通过URLHttpConnection方式来取得图片,并且显示在ImageView上
- Java IO: 网络
- 同行不同命:中美两国的“网络水军”有哪些差异
- openfire因为网络不稳定而造成消息丢失的解决方案
- TCP/IP
- IdHttpServer实现webservice
- HTTPS 101
- HTTP相关概念
- IOS网络笔记--地图内容1
- Java之网络爬虫WebCollector+selenium+phantomjs(三)
- http post/get 2种使用方式
- http post/get 2种使用方式
- 网络营销广告的类型(CPC、CPM、CPA、CPS, CTR)
- 卷积神经网络中卷积概念的理解
- iOS网络请求---1
- linux socket 设置从哪个网络设备发送数据 SO_BINDTODEVICE
- android 判断当前网络是否可用
- 通过URLHttpConnection方式连接网络步骤,获取位图为例
- Tomcat部署成Https模式的方法