保持登录状态方法我是使用cookies然后通过httppost.addHeader确保每次操作会被服务器认定为同一个用户在操作,通过url后面直接加参数可行,但是中文会出现乱码,(没有设置UTF_8的缘故),尝试了NameValuePair和JSONObject通过httppost.setEntity添加对应值并设置了UTF_8格式,却获得不到应有的值,服务器返回101(正确的话会返回100,101代表参数值不对或者没有登录)(个人推测是参数不对导致的),本人刚接触http,不对之处请各位大大见谅并拉我一把,万分感谢。代码如下:
1 HttpClient httpclient = new DefaultHttpClient(); 2 HttpPost httppost = new HttpPost(xxburl); 3 // xxburl += "idReceiveAccountInfo=" + idReceiveAccountInfo+"&content="+content;//可行,但中文出现乱码,应该是没有设置UTF_8 4 5 // List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 6 // nameValuePairs.add(new BasicNameValuePair("idReceiveAccountInfo", "idReceiveAccountInfo")); 7 // nameValuePairs.add(new BasicNameValuePair("content", "content")); 8 // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8)); 9 10 httppost.addHeader("Accept", "application/json"); 11 httppost.addHeader("Content-Type", "application/json"); 12 httppost.addHeader("Cookie", COOKIE); 13 14 JSONObject obj1=new JSONObject(); 15 obj1.put("idReceiveAccountInfo", idReceiveAccountInfo); 16 obj1.put("content", content); 17 18 httppost.setEntity(new StringEntity(obj1.toString(), HTTP.UTF_8));19 20 HttpResponse response; 21 response = httpclient.execute(httppost);
程序没有报错,所以就不贴LOGCAT了。
url编码
能不能给个例子?
@風过无痕: 试试 这个 HttpUtility.HtmlEncode("") 要引用System.Web 的