試試這個IT賣場名PASS用戶和會話到共享PREF
app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
userid=app_preferences.getLong("userid",0);
username=app_preferences.getString("username", "");
password=app_preferences.getString("password", "");
session=app_preferences.getString("session", "");
class task extends AsyncTask<String, integer, Boolean>{
//httpreqclass htrq= new httpreqclass();
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
showProgressDialog();
}
@Override
protected Boolean doInBackground(String... params) {
String usr=params[0];
String p=params[1];
String data[]=new String[2];
String ur= "http://192.168.1.45/Android.asmx/Login";
// TODO Auto-generated method stub
data= postData(usr,p,ur);
boolean sucess=false;
try {
JSONObject jso = new JSONObject(data[0]);
sucess =jso.getBoolean("isSucceeded");
if (sucess) {
SharedPreferences.Editor editor = app_preferences.edit();
JSONObject jsr= jso.getJSONObject("result");
Long a= jsr.getLong("Id");
editor.putLong("userid", a);
editor.putString("username", usr);
editor.putString("password", p);
editor.putString("session", data[1]);
editor.commit(); // Very important
}
}catch (Exception e){
}
return sucess;
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
here put code what you want to do next
}
}
public String[] postData(String u, String p,String url) {
String asp = null,data = null;
String[] rtrn=new String[2];
//Log.e("i entrd here", "");
try {
HttpClient httpclient = new DefaultHttpClient(getHttpParams());
HttpPost httppost = new HttpPost();
HttpResponse response;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("EmailId", u));
params.add(new BasicNameValuePair("PassWord", p));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
httppost.setEntity(ent);
response = httpclient.execute(httppost);
Header[] headers;
headers = response.getHeaders("Set-Cookie");
//Header atha= respone.getFirstHeader("PHPSESSID");
List<Cookie> cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.d("TAG","no cookies received");
} else {
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().contentEquals("ASP.NET_SessionId")) {
asp = cookies.get(i).getValue();
}
}
Log.e("this is the cookiee", asp);
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
Log.e("", sb.toString());
rtrn[0]=sb.toString();
rtrn[1]=asp;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rtrn;
}
private HttpParams getHttpParams() {
HttpParams htpp = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(htpp, 3000);
HttpConnectionParams.setSoTimeout(htpp, 5000);
return htpp;
}
你可以參考代碼,使應用程序按照您的邏輯
,並注意接下來的時候起,當你發送請求到服務器不要忘記添加會話/ cookie標頭
首先我應該說,非常感謝您花時間編寫代碼。其次,由於幾乎沒有問題,我無法運行代碼。例如,我找不到httpreqclass,pDlg,fetchdata,showProgressDialog ...... 第三,是否有任何形式的漫遊(或者可能是使用上述結構的最終方法)。在github上爲每個人提供一個工作版本可能很有用(或者我最終可以自己做,因爲我認爲這對許多人有用)。 再次感謝 – Sina
sry fr that ...我會把它放在git hub nd在這裏添加thnks –
嗨我編輯了代碼不要忘記更改url和參數到相應的1 s,您有權訪問 –