【发布时间】:2017-11-16 12:57:51
【问题描述】:
我正在尝试将图像上传到三星 j7 相机中。我得到一些带有</div> 标签的服务器响应。
如何解决此错误以及从响应中获取图像的方法是什么?
错误图像表单 Android 服务器响应
我的代码
-
受保护的无效 Upload_Server() { // TODO 自动生成的方法存根 字符串 urlServer = null; System.out.println("调用后进度"); 试试{
Log.e("Image Upload", "Inside Upload"); HttpURLConnection connection = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; String pathToOurFile = imagepath; // String pathToOurFile1 = imagepathcam; System.out.println("Before Image Upload" + imagepath); if(Videoboolean){ urlServer = Constants.IMAGEVIDEOURL+"videopostUpload/"; }else { urlServer = Constants.IMAGEVIDEOURL+"imagepostUpload/"; } System.out.println("URL SETVER" + urlServer); System.out.println("After Image Upload" + imagepath); String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1*1024*1024; System.out.println("enter the file path in android"+pathToOurFile); FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); // FileInputStream fileInputStream1 = new FileInputStream(new File(pathToOurFile1)); URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); System.out.println("URL is "+url); System.out.println("connection is "+connection); // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); outputStream = new DataOutputStream( connection.getOutputStream() ); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); outputStream.writeBytes(lineEnd); System.out.println("enter the image upload response"+outputStream.getClass().getCanonicalName()); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // Read file bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { outputStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) int serverResponseCode = connection.getResponseCode(); String serverResponseMessage = connection.getResponseMessage(); System.out.println("image"+serverResponseMessage); fileInputStream.close(); outputStream.flush(); outputStream.close(); DataInputStream inputStream1 = null; inputStream1 = new DataInputStream (connection.getInputStream()); String str=""; String Str1_imageurl=""; while (( str = inputStream1.readLine()) != null) { Log.e("Debug","Server Response "+str); Str1_imageurl = str; Log.e("Debug","Server Response String imageurl"+str); } inputStream1.close(); System.out.println("image url"+Str1_imageurl); PostImVD = Str1_imageurl.trim(); JSONArray array = new JSONArray(PostImVD); JSONObject jsonObj = array.getJSONObject(0); if(Videoboolean){ ImageVideo = jsonObj.optString("video_name"); }else { ImageVideo = jsonObj.optString("image_name"); } System.out.println("Profile Picture Path" + PostImVD); System.out.println("Profile Picture Path" + ImageVideo); } catch(Exception e){ e.printStackTrace(); }};
【问题讨论】:
-
服务器响应显然不是 JSON。然后您尝试将其转换为 JSONArray
-
另外,请格式化您的问题,不要在整个问题中使用 bold,这让我不想帮助您。
-
我不介意看到
javascript、php和html-tags 发挥作用 -
我转换了 JSONArray 但无法得到结果
标签: javascript java php android html