【问题标题】:Value < of type java.lang.String cannot be converted to JSONArray In Androidjava.lang.String 类型的值 < 在 Android 中无法转换为 JSONArray
【发布时间】:2017-11-16 12:57:51
【问题描述】:

我正在尝试将图像上传到三星 j7 相机中。我得到一些带有&lt;/div&gt; 标签的服务器响应。

如何解决此错误以及从响应中获取图像的方法是什么?

错误图像表单 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();
    
    }
    

    };

    1:Error Image Form Android Server Response

【问题讨论】:

  • 服务器响应显然不是 JSON。然后您尝试将其转换为 JSONArray
  • 另外,请格式化您的问题,不要在整个问题中使用 bold,这让我不想帮助您。
  • 我不介意看到 javascriptphphtml-tags 发挥作用
  • 我转换了 JSONArray 但无法得到结果

标签: javascript java php android html


【解决方案1】:

首先,尝试在此页面中验证您的 JSON 响应,例如:

https://jsonlint.com/

如果您的 JSON 正确,则确定是数组还是对象,取决于使用正确的解析:

如果是数组:

 JSONArray array = new JSONArray(your_string_json);

如果它是一个对象

 JSONObject object = new JSONObject(your_string_json);

【讨论】:

  • 好的,我试试这个,但是我如何删除
    标签,因为响应格式是
    [{"status":"Success","imageurl":"http: \/\/138.68.151.147\/images\/users\/8a1c38f057eafa00c6ae5d1ff3a4c6ff.jpg","image_name":"8a1c38f057eafa00c6ae5d1ff3a4c6ff.jpg"}]
  • 你不应该那样做,服务器必须返回一个有效的内容响应,但是如果你想解析,尝试使用 string.substring(6)
  • 你能给我拆分的参考代码吗,因为我无法拆分它
  • 字符串值 = "your_string_with_div";字符串解析 = value.substring(6);如果您不确定,请打印解析:)
  • 猜你喜欢
    • 1970-01-01
    • 2016-02-21
    • 2013-04-03
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多