【发布时间】:2014-01-03 11:52:25
【问题描述】:
我正在尝试使用 php + mysql 数据库/java for android 制作一个简单的登录系统。 两个即时通讯使用Json。
我在这里有 php 代码:(我想要归档的一个简单示例)。
<?php
include("../dbfunctions.php");
class mobilelogin{
private $user;
private $pass;
public function getData(){
$get->user = $_POST['user'];
$get->pass = $_POST['pass'];
return $get;
}
public function getDbData($user){
$dbfunctions = new dbfunctions;
$getvalue = $this->getData();
return $dbfunctions->returnUser($user); // get pass by user
}
public function md5convert($user){
return md5($this->getDbData($user)); // convert this pass to md5
}
public function check(){ // check everthing here
$getvalue = $this->getData();
if(isset($getvalue->user) && isset($getvalue->pass)) // everthing filled in
{
return "valid";
}else{
return "invalid";
}
}
public function response(){ // show data here in json format
echo json_encode(array('result' => $this->check()));
}
}
header("Content-Type: text/json");
$mobilelogin = new mobilelogin;
echo $mobilelogin->response();
?>
如您所见,我只尝试了一个简单的 json 帖子示例,
我在 json 中的结果是这样的:{ result:invalid }
如您所见,如果 php 代码从 java 接收到 post 值,它应该显示有效,否则无效。
现在我有了这个 android 代码,它执行一个 post 请求:
public void login(View v) throws ClientProtocolException, URISyntaxException, IOException, JSONException
{
HttpPost httppost = new HttpPost(processUrl);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", uservalidated));
nameValuePairs.add(new BasicNameValuePair("pass", passvalidated));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
client.execute(httppost);
Toast.makeText(getBaseContext(),"Everthing well.",Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("testnow", response());
}
这会在您单击登录按钮时触发发布请求,并且 php 服务器应该会收到它,
就像你看到的 Log (Log.e("testnow", response());) 是通过 httpget 接收数据的函数,这个函数:
public String response() throws URISyntaxException, ClientProtocolException, IOException, JSONException{
HttpGet request = new HttpGet();
request.setURI(new URI(processUrl));
HttpResponse response;
response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
String line;
while((line=in.readLine())!=null)
{
builder.append(line);
}
String JSONdata = builder.toString();
JSONObject jObject = new JSONObject(JSONdata);
String JSONresponse = jObject.getString("result");
return JSONresponse;
}
这从 json 输出中显示无效。但它应该显示有效,当我在用户名和密码中打勾时,它不会更新。
希望您能理解我的问题,并欢迎所有帮助。 另外,如果这不是以这种方式制作登录系统的正确方法,也请分享。
【问题讨论】:
标签: java php android mysql json