您可以使用Active DefaultHttpClient
連接,並在每個移動請求驗證,在服務器端,通過$_SESSION
。這裏是一個小示例代碼(不用於生產)
首先出現的是一類:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class NetClient {
private HttpClient client = null;
public NetClient() {
client = new DefaultHttpClient();
}
public String request(String url) throws ClientProtocolException, IOException {
HttpPost post = new HttpPost(url);
HttpResponse res = client.execute(post);
BufferedReader br = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
String data = "";
String line = "";
while ((line = br.readLine()) != null) {
data = data + line;
}
return data;
}
public String login(String url) throws ClientProtocolException, IOException {
HttpPost post = new HttpPost(url);
ArrayList pa = new ArrayList();
pa.add(new BasicNameValuePair("username", "admin"));
pa.add(new BasicNameValuePair("password", "admin"));
post.setEntity(new UrlEncodedFormEntity(pa, "UTF-8"));
HttpResponse res = client.execute(post);
BufferedReader br = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
String data = "";
String line = "";
while ((line = br.readLine()) != null) {
data = data + line;
}
return data;
}
}
在mainactivity.java
然後
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
NetClient n = new NetClient();
String k = n.login("http://x.com/testAREA/securetrans/login.php");
Log.w("login result", k);
String l = n.request("http://x.com/testAREA/securetrans/content.php");
Log.w("ask if logged in", l);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
凡的login.php
<?php
session_start();
if(isset($_POST['username']))
{
$username = $_POST['username'];
$password = $_POST['password'];
if($username == 'admin' && $password == 'admin')
{
$_SESSION['username'] = $username;
echo "admin setted";
exit;
}
else
{
echo "-1";
}
}
?>
and content.php爲:
<?php
session_start();
if(isset($_SESSION['username']))
{
echo "login ok";
}
else
{
echo "not login";
}
?>
當我們運行此示例代碼將在與
echo "login ok";
更多信息結束:http://www.pipiscrew.com/2014/11/phpandroid-secure-connection-with-session-variable/
你的意思是我要發送的唯一ID的URL進行進一步的要求。 – 2011-05-10 09:38:35
確切地說,你的服務器將知道它需要獲取什麼會話以查看用戶是否已通過身份驗證。這通常是一個cookie,您需要重新發送每個請求。看看這裏:http://stackoverflow.com/questions/3587254/how-do-i-manage-cookies-with-httpclient-in-android-and-or-java – Yahel 2011-05-10 09:49:27
但有一件事,同樣的事情工作在瀏覽器和iPhone上。所以,這個問題只針對android和爲什麼。你能提出一些想法嗎? – 2011-05-10 09:51:47