【已解决】 HTTP网络编程没出来结果,打的代码感觉跟拉锯哥的基本是一样的除了变量名
private void postSingleFile() {
new Thread(new Runnable() {
@Override
public void run() {
BufferedInputStream bis = null;
BufferedReader br = null;
OutputStream outputStream = null;
try {
@SuppressLint("SdCardPath") File filePng = new File("/sdcard/Download/browser/e1045e8079daa721f7309b4a61fdc1cb.jpg");
String fileKey = "file";
String fileName = filePng.getName();
String fileType = "image/jpeg";
URL url = new URL(BASE_URL + "/file/upload");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("User-Agent", "Android/" + Build.VERSION.SDK_INT);
httpURLConnection.setRequestProperty("Accept", "*/*");
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
//准备数据
StringBuilder headerInfo = new StringBuilder();
headerInfo.append("--");
headerInfo.append(BOUNDARY);
headerInfo.append("\r\n");
headerInfo.append("Content-Disposition: form-data; name=\"" + fileKey + "\"; filename=\"" + fileName + "\"");
headerInfo.append("\r\n");
headerInfo.append("Content-Type: " + fileType);
headerInfo.append("\r\n");
headerInfo.append("\r\n");
byte[] headerInfoBytes = headerInfo.toString().getBytes("UTF-8");
//写入头部信息
outputStream = httpURLConnection.getOutputStream();
outputStream.write(headerInfoBytes);
//写入文件
FileInputStream fis = new FileInputStream(filePng);
bis = new BufferedInputStream(fis);
byte[] buffer = new byte[1024];
int len;
while((len = bis.read(buffer,0,buffer.length)) != -1){
outputStream.write(buffer,0,len);
}
//写入尾部信息
StringBuilder footerInfo = new StringBuilder();
footerInfo.append("\r\n");
footerInfo.append("--");
footerInfo.append(BOUNDARY);
footerInfo.append("--");
footerInfo.append("\r\n");
footerInfo.append("\r\n");
outputStream.write(footerInfo.toString().getBytes("UTF-8"));
outputStream.flush();
//获取返回结果
int responseCode = httpURLConnection.getResponseCode();
Log.v(TAG,"responseCode --------> " + responseCode);
if(responseCode == 200){
InputStream is = httpURLConnection.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
String dataReturned = br.readLine();
Log.v(TAG,dataReturned);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
流程应该也没问题,不知道为什么出不来log,下面是点击了按钮之后的log

问题解决了,是jar包的问题,换成了之前的jar包就可以辽
您不能打断点调试一下?你这啥log都没输出。