Android从远程数据库登录和解析数据

时间:2012-03-18 00:37:21

标签: php android mysql

当我执行这两个代码时,catLog中出现此错误:

  

03-18 01:05:16.602:E / log_tag(788):解析数据时出错   org.json.JSONException:

的字符0处的输入结束

在PHP中我收到此错误:

  

[18-Mar-2012 00:03:54 UTC] PHP Parse错误:语法错误,意外   第11行/home/cyberite/public_html/khacheb/sig1.php中的T_STRING

php代码:

<?php
  /**
   * Database config variables
   */
  $username = $_POST['username'];  
  $password = $_POST['password']; 

  mysql_connect("localhost","cyberite","WAJDI@NASRAOUI@");
  mysql_select_db("cyberite_khacheb");

  $query = mysql_query(“SELECT cin,accountNb,username , password FROM users WHERE    username = ‘$username’ AND password = ‘$password’”);  
  $result = mysql_query($query) or die(“Unable to verify user because : ” . mysql_error());
  while($row=mysql_fetch_assoc($query))
    $output[]=$row;
  print(json_encode($output));
  mysql_close();
?>

此处在Android代码中:

package com.pfe.pfe;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class signin1 extends Activity {

  public static final String strURL = "http://cyberi-tech.com/khacheb/sig1.php";

  StringBuilder sb=null;

  TextView  inlog;
  TextView inpass;
  Button log;
  Button cancel;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signin);

    inlog = (TextView)findViewById(R.id.inputUsername);
    inpass = (TextView)findViewById(R.id.inputPassword);
    log = (Button)findViewById(R.id.Login);
    cancel = (Button)findViewById(R.id.sigCancel);

    log.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        InputStream is = null;
        String result = "";

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username", inlog.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("password", inpass.getText().toString()));

        try{
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(strURL);
          httppost.setEntity(new  UrlEncodedFormEntity(nameValuePairs));
          HttpResponse response = httpclient.execute(httppost);
          HttpEntity entity = response.getEntity();
          is = entity.getContent();
        }
        catch(Exception e){
          Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convertion de la requête en string
        try{
          BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
          }
          is.close();
          result=sb.toString();
        }
        catch(Exception e){
          Log.e("log_tag", "Error converting result " + e.toString());
        }

        try{
          JSONArray jArray = new JSONArray(result);
          for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            Log.i("log_tag","cin: "+json_data.getInt("cin")+
              ", accountNb: "+json_data.getString("accountNb")+
              ", username: "+json_data.getString("username")+
              ", password: "+json_data.getString("password")
            );
          }
        }
        catch(JSONException e){         
          Log.e("log_tag", "Error parsing data " + e.toString());
        }       
      }
    });

    cancel.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        finish();   
      }
    });

  }
}

1 个答案:

答案 0 :(得分:0)

确保您获得$_POST数据。并始终在sql查询中使用它之前过滤数据以解决安全问题。

<?php
/**
* Database config variables
*/
$username = isset($_POST['username']) ? $_POST['username'] : '';  
$password = isset($_POST['password']) ? $_POST['password'] : ''; 

if(strlen($username) && strlen($password)) {

mysql_connect("localhost","cyberite","WAJDI@NASRAOUI@");
mysql_select_db("cyberite_khacheb");

$username = mysql_real_escape($username);
 $password = mysql_real_escape($password);

$query = mysql_query(“SELECT cin,accountNb,username , password FROM users WHERE    username = ‘$username’ AND password = ‘$password’”);  
$result = mysql_query($query) or die(“Unable to verify user because : ” . mysql_error());
while($row=mysql_fetch_assoc($query)) {
  $output[]=$row;
}
print(json_encode($output));
mysql_close();
}
} //end of if
?>