1、get 用 IntentService请求
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
HttpUtil.getInstance().get();
}
public void get(){
try {
URL url = new URL("http://43.143.146.165:7777/foods/getFoods?currentPage=1&pageSize=12");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(10*1000);
httpURLConnection.setReadTimeout(10*1000);
httpURLConnection.connect();
if(httpURLConnection.getResponseCode()==200){
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String str="";
while ((str= bufferedReader.readLine())!=null){
stringBuilder.append(str);
}
Log.d("TAG", "zzz"+stringBuilder.toString());
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (ProtocolException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
2、用POST 线程请求
public class MyThread2 extends Thread{
String name;
public MyThread2(String name) {
this.name = name;
Log.i("MyThread2", "MyThread2");
}
@Override
public void run() {
super.run();
HttpUtil.getInstance().post(name);
}
}
public void post(String name){
try {
URL url = new URL(name);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(10*1000);
connection.setReadTimeout(10*1000);
connection.setDoOutput(true);
connection.setDoInput(true);
String params="pageSize=10¤tPage=1";
OutputStream outputStream = connection.getOutputStream();
outputStream.write(params.getBytes());
outputStream.flush();
if (connection.getResponseCode()==200){
InputStream inputStream = connection.getInputStream();
byte[] bytes = new byte[1024];
int len=0;
StringBuilder stringBuilder = new StringBuilder();
while ((len=inputStream.read(bytes))!=-1){
stringBuilder.append(new String(bytes,0,len));
}
String s = stringBuilder.toString();
Log.d("--zcx","run:"+s);
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
下载请求
上传请求
public void upload(String urlpath,String filepath){
// 设置边界
String BOUNDARY = UUID.randomUUID().toString();
//前面边界符合﹔结尾符号
String PREFIX = "--" , LINE_END = "\r\n";
// http类型
String CONTENT_TYPE = "multipart/form-data";
try {
//连接
URL url = new URL(urlpath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(10*1000);
connection.setReadTimeout(10*1000);
connection.setDoInput(true);
connection.setDoOutput(true);
//特殊要求
connection.setRequestProperty("Content-Type", CONTENT_TYPE+";boundary="+BOUNDARY);
connection.connect();
//向外界服务器
File file = new File(filepath);
if (file!=null) {
OutputStream outputStream = connection.getOutputStream();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(PREFIX);
stringBuffer.append(BOUNDARY);
stringBuffer.append(LINE_END);
stringBuffer.append("Content-Disposition: form-data; name=\"file\"; filename=\""+file.getName()+"\""+LINE_END);
stringBuffer.append(LINE_END);
outputStream.write(stringBuffer.toString().getBytes());
//开始写真实数据
InputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len=0;
while ((len =inputStream.read(bytes))!=-1){
outputStream.write(bytes, 0, len);
}
inputStream.close();
//开始写结束边界
outputStream.write(LINE_END.getBytes());
byte[] end=(PREFIX+BOUNDARY+PREFIX+LINE_END).getBytes();
outputStream.write(end);
outputStream.flush();
if (connection.getResponseCode()==200){
}
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (ProtocolException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
HFS的设置