IllegalStateException: Content has been consumed

前端 未结 4 2024
你的背包
你的背包 2020-12-08 10:17

I got struck because of IllegalStateException in the following code. Can anybody please help me? Code:

import java.io.BufferedReader;
import jav         


        
相关标签:
4条回答
  • 2020-12-08 10:29

    I just dealt with a case of a null check on the entity causing it to be flagged as "consumed". Hope my headache will help someone else out there.

    Vikas Patidar's answer helped me figure out the key to the riddle, so many thanks

    0 讨论(0)
  • 2020-12-08 10:35

    You can consume Content only at once from an Entity

    in the line :

    final JSONObject jObject = new JSONObject(EntityUtils.toString(resEntity));
    

    you have consumed content and again you are using the same at here:

    Log.e("XXX",EntityUtils.toString(resEntity));
    

    That why it is causing IllegalStateException: Content has been consumed

    So the solution is here:

    String _response=EntityUtils.toString(resEntity); // content will be consume only once
    final JSONObject jObject=new JSONObject(_response);
    Log.e("XXX",_response);
    
    0 讨论(0)
  • 2020-12-08 10:37

    it's also happens if you are writing the consuming statement in the Expressions of the debugger!!!
    (e.g if you are doing "watch" to something like EntityUtils.toString(resEntity))

    0 讨论(0)
  • 2020-12-08 10:46

    First, this has to be a mistake that every single new android programmer makes and it's asked here every single day. You have

    user.getText().toString()!= ""&& pw.getText().toString()!= ""
    

    This doesn't do what you want it to. You need

     !user.getText().toString().equals("")&& !pw.getText().toString().equals("")
    

    Also, you need to print the stacktrace. In your exception, you need

    e.printStackTrace()
    

    instead of logging

    e.toString()
    
    0 讨论(0)
提交回复
热议问题