我试图从URL解析JSON数据,当我尝试建立连接时,它抛出异常的 java.net.ProtocolException:响应已读如何在android中使用HttpUrlConnection类添加cookie到url?
我以后不能写请求主体得到响应消息为未找到。
我检查了网页浏览器中的url,它显示了当我登录wuth我的凭据时的Json数据。
所以,我发现我需要添加cookie到我的连接,但我不知道如何做到这一点。
public void parseData(String cookie){
HttpUrlConnection connection;
try{
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Cookie", cookie);
Log.e(TAG, "cookie " + cookie);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
Log.e(TAG,connection.getResponseMessage());
/**
here i'm trying to parse the data
using BufferedReader calss
**/
}
catch(IOException e){}
}
我需要在连接中添加cookie。 请帮助我。
可能重复:http://stackoverflow.com/a/16171708/4310784 –
我也试过这种方式。但是我得到一个异常,因为“java.net.ProtocolException:在读取响应之后无法写入请求主体”能告诉我为什么会抛出这个异常。 @Mohammad Z – Madhan
@Madhan:对不起,删除了我的答案,因为我意识到我错了。 – Tigger