1.post请求进行上传方式
private GsonGet() {
}
private static GsonGet gsonGet=null;
public static GsonGet getInstance(){
if (gsonGet==null){
gsonGet=new GsonGet();
}
return gsonGet;
}
public void post(String path){
try {
URL url = new URL(path);
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(10*1000);
httpURLConnection.setReadTimeout(10*1000);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
String params="pageSize=10¤tPage=1";
OutputStream outputStream=httpURLConnection.getOutputStream();
outputStream.write(params.getBytes());
outputStream.flush();
if (httpURLConnection.getResponseCode()==200){
InputStream inputStream=httpURLConnection.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.i("--post", "run: "+s);
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void download(String url1){
try {
URL url = new URL(url1);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(10*1000);
httpURLConnection.setReadTimeout(10*1000);
if (httpURLConnection.getResponseCode()==200){
InputStream inputStream=httpURLConnection.getInputStream();
byte[]bytes=new byte[1024];
int len=0;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File path1=Environment.getExternalStorageDirectory();
File file=new File(path1,"w.jpg");
FileOutputStream fileOutputStream=new FileOutputStream(file);
while ((len=inputStream.read())!=-1){
fileOutputStream.write(bytes,0,len);
}
}
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void upload(String url1,String file){
String BOUNDARY = UUID.randomUUID().toString();
String PREFIX = "--" , LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data";
try {
URL url = new URL(url1);
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(10*1000);
httpURLConnection.setReadTimeout(10*1000);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Content-Type",CONTENT_TYPE+";boundary="+BOUNDARY);
httpURLConnection.connect();
File file1=new File(file);
if (file1!=null){
OutputStream outputStream=httpURLConnection.getOutputStream();
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append(PREFIX);
stringBuffer.append(BOUNDARY);
stringBuffer.append(LINE_END);
stringBuffer.append("Content-Disposition: form-data; name=\"file\"; filename=\""+file1.getName()+"\""+LINE_END);
stringBuffer.append(LINE_END);
outputStream.write(stringBuffer.toString().getBytes());
InputStream inputStream=new FileInputStream(file1);
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(LINE_END.getBytes());
outputStream.write(end);
outputStream.flush();
if (httpURLConnection.getResponseCode()==200){
}
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
2.Get请求方式
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
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){
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String str="";
while ((str=bufferedReader.readLine())!=null){
stringBuilder.append(str);
}
String json=stringBuilder.toString();
Log.i("fds", "onHandleIntent: "+json);
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
2316





