我将图片从Android应用上传到服务器,并作为服务器的响应我发送JSON值。
如何在我的应用中获取该JSON?
HttpURLConnection httpURLConnection;
DataOutputStream dataOutputStream;
...
File file = new File(path);
if (file.isFile()) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL("http://something.com/upload.php");
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
httpURLConnection.setRequestProperty("File", path);
dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + path + "\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dataOutputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = httpURLConnection.getResponseCode();
fileInputStream.close();
dataOutputStream.flush();
dataOutputStream.close();
}
return true;
}
else {
return false;
}
}
通常当我使用httpClient
时,我会用
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("utf-8")), 8);
String line;
jsonValue = "";
while ((line = reader.readLine()) != null) {
jsonValue += line;
}
jsonObject = new JSONObject(jsonValue);
答案 0 :(得分:1)
尝试使用以下代码从 HttpUrlConnection
中读取响应/错误HttpURLConnection httpURLConnection;
DataOutputStream dataOutputStream;
...
File file = new File(path);
if (file.isFile()) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL("http://something.com/upload.php");
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
httpURLConnection.setRequestProperty("File", path);
dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + path + "\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dataOutputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = httpURLConnection.getResponseCode();
if(serverResponseCode!=200{
//error occurred
BufferedReader in=new BufferedReader(new
InputStreamReader(
httpURLConnection.getErrorStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
String errorString=sb.toString();
}else{
BufferedReader in=new BufferedReader(new
InputStreamReader(
connection.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
String response=sb.toString();
Log.d(TAG,sb.toString());
in.close();
}
fileInputStream.close();
dataOutputStream.flush();
dataOutputStream.close();
//do whatever you wish to do with the error/response string then
}
return true;
}
else {
return false;
}
但是,我建议你使用Volley或Retrofit作为你的Http客户端,因为它们提供了比HttpUrlConnection和AsyncTask更多的额外优势。我个人更喜欢Retrofit 2,因为它给了我很多优势,包括缓存,分段上传/下载,日志记录以及更快的响应