android–java.io.IOException这个错误的原因是什么:Content-Length和流长度不一致

android–java.io.IOException这个错误的原因是什么:Content-Length和流长度不一致,第1张

概述我收到了这个错误java.io.IOException:Content-Lengthandstreamlengthdisagree在这行代码中返回response.body().bytes();这是完整的代码编辑:在一些谷歌之后,错误的原因是从okhttplibif(contentLength!=-1L&&contentLength!=bytes.size.toLong()){thro

我收到了这个错误

   java.io.IOException: Content-Length and stream length disagree

在这行代码中返回response.body().bytes();

这是完整的代码

编辑:在一些谷歌之后,错误的原因是从okhttp lib

if (contentLength != -1L && contentLength != bytes.size.tolong()) {  throw IOException(      "Content-Length ($contentLength) and stream length (${bytes.size}) disagree")}

但是如何解决这个问题?

编辑:

这是完整的代码:

public class OkhttpHandler extends AsyncTask<VoID, VoID, byte[]> {    private final String Fetch_URL = "http://justedhak.comlu.com/get-data.PHP";    ArrayList<Listitem> Listitem;    int resulta;    OkhttpClIEnt httpClIEnt = new OkhttpClIEnt();    String myJsON;    JsONArray peoples = null;    inputStream inputStream = null;    @OverrIDe    protected byte[] doInBackground(VoID... params) {        Log.d("e", "dddddddddd");        Log.d("e", Fetch_URL);        Request.Builder builder = new Request.Builder();        builder.url(Fetch_URL);        Request request = builder.build();        String result = null;        try {            Response response = httpClIEnt.newCall(request).execute();          //  int statusCode = response.getStatusline().getStatusCode();              int statusCode =200;           // httpentity entity = response.body().byteStream();            if (statusCode == 200) {                byte[] buffer = new byte[8192];                int bytesRead;                ByteArrayOutputStream output = new ByteArrayOutputStream();                while ((bytesRead = inputStream.read(buffer)) != -1) {                    output.write(buffer, 0, bytesRead);            //    inputStream = response.body().byteStream();                // Json is UTF-8 by default             //   BufferedReader reader = new BufferedReader(new inputStreamReader(inputStream, "UTF-8"), 8);            /*    StringBuilder sb = new StringBuilder();                String line = null;                while ((line = reader.readline()) != null)                {                    sb.append(line + "\n");                }                result = sb.toString();*/                resulta = 1; //"Success                Log.d("e", "response.");                return response.body().bytes();            }            else            {                resulta = 0; //"Failed            }        } catch (Exception e) {            Log.d("e", "r2r2 error");            e.printstacktrace();        }        finally {            try{if(inputStream != null)inputStream.close();}catch(Exception squish){}        }        return null;    }    protected voID onPostExecute(String result){        if( resulta ==1){            myJsON=result;            showList();        }        else{            Log.e("d","zzzzzzzz");        }    }    protected voID showList(){        try {            Log.e("d","jjjjjjjjjj");            JsONObject JsonObj = new JsONObject(myJsON);            peoples = JsonObj.getJsONArray("result");            Listitem = new ArrayList<Listitem>();            for(int i=0;i<peoples.length();i++){                JsONObject c = peoples.getJsONObject(i);                String ID = c.getString("ID");                String url = c.getString("path");                Listitem.add(new Listitem(ID,url));                Log.e("d","ppppp");            }         //   GrIDVIEwAdapter adapter = new GrIDVIEwAdapter(this, R.layout.grID_item_layout, Listitem);            //   grIDVIEw.setAdapter(grIDAdapter);           // adapter.notifyDataSetChanged();            //  List.setAdapter(adapter);        } catch (JsONException e) {            e.printstacktrace();        }    }}

解决方法:

抛出异常因为你调用了inputStream inputStream = response.body().byteStream();然后调用response.body().bytes();再次.

您可以使用inputStream中的return bytes数组或返回result.getBytes();相反,如果这是你想要返回的.

从inputStream到bytes,请参阅以下内容:

    public byte[] getBytesFrominputStream(inputStream inputStream) throws IOException {        try {                        byte[] buffer = new byte[8192];            int bytesRead;            ByteArrayOutputStream output = new ByteArrayOutputStream();            while ((bytesRead = inputStream.read(buffer)) != -1) {                output.write(buffer, 0, bytesRead);            }            return output.toByteArray();        } catch (OutOfMemoryError error) {            return null;        }    }

更新:

如果您在ResponseBody.class上调试,您将看到以下屏幕截图:

总结

以上是内存溢出为你收集整理的android – java.io.IOException这个错误的原因是什么:Content-Length和流长度不一致全部内容,希望文章能够帮你解决android – java.io.IOException这个错误的原因是什么:Content-Length和流长度不一致所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1099576.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存