Android的cookie获取和上传问题!!!!

曹逗逗 发布于 2016/11/26 15:01
阅读 408
收藏 0
11-26 14:54:17.532: E/AndroidRuntime(15031): FATAL EXCEPTION: Thread-8847
11-26 14:54:17.532: E/AndroidRuntime(15031): Process: com.example.demoradiofragment, PID: 15031
11-26 14:54:17.532: E/AndroidRuntime(15031): java.lang.IllegalStateException: Cannot set request property after connection is made
11-26 14:54:17.532: E/AndroidRuntime(15031): at com.android.okhttp.internal.huc.HttpURLConnectionImpl.setRequestProperty(HttpURLConnectionImpl.java:537)
11-26 14:54:17.532: E/AndroidRuntime(15031): at com.example.demoradiofragmentn.Fragment_C.qingqiu1(Fragment_C.java:236)
11-26 14:54:17.532: E/AndroidRuntime(15031): at com.example.demoradiofragmentn.Fragment_C$MyThreadc.run(Fragment_C.java:204)
11-26 14:54:17.532: E/AndroidRuntime(15031): at java.lang.Thread.run(Thread.java:818)

上边的是报错

我的cookie的获取:

cookieval = conn.getHeaderField("set-cookie");
System.out.println("cookieval        "+cookieval);

if(cookieval != null) { 
sessionid = cookieval.substring(0, cookieval.indexOf(";")); 
}
 
我的cookie上传:

 public String qingqiu(String buf)
 {
 URL url = null;
try {
url = new URL(buf);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn= (HttpURLConnection) url.openConnection();
if(Login.sessionid!=null)
  {
  conn.setRequestProperty("cookie", Login.sessionid); //cookie上传
  }
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
  try {
json2=HttpUtils.sendGet(conn);//GET请求
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
return json2;
 }
我的http请求

public class HttpUtils {
    static Context context;
static String sessionid;
public static byte[] data1;
    public static String JSPSESSID = null;


    public static String sendGet(HttpURLConnection conn) {
   
   
    String jsonString = null;
    try{          
   conn.setRequestMethod("GET"); 
int code = conn.getResponseCode();
if (code == 200)// 接收json
{
ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
byte[]  data = new byte[1024];
int len = 0;
try
{
while ((len = conn.getInputStream().read(data)) != -1)
{
outPutStream.write(data, 0, len);
}
//String jsonString;
data1=outPutStream.toByteArray();
    jsonString = new String(outPutStream.toByteArray(), "gb2312");  
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
System.out.println("RequestMethod failed! code = "+ conn.getResponseCode());
}      
 } catch (Exception e) {  
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        return jsonString;
}
}

加载中
返回顶部
顶部