2012-03-18 9 views
0

私はこれらの2つのコードを実行すると、catLogでこのエラーが表示されます:Androidのログインおよびリモート・データベースからのデータを解析

03-18 01:05:16.602: E/log_tag(788): Error parsing data org.json.JSONException: End of input at character 0 of

やPHPで、私はこのエラーを取得:

[18-Mar-2012 00:03:54 UTC] PHP Parse error: syntax error, unexpected T_STRING in /home/cyberite/public_html/khacheb/sig1.php on line 11

PHPをコード:ここではAndroidのCで

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

    mysql_connect("localhost","cyberite","[email protected]@"); 
    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(); 
?> 

ODE:

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(); 
     } 
    }); 

    } 
} 

答えて

0

はuが$_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","[email protected]@"); 
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 
?> 
+0

エラーがデータを解析するときに – midok14

+0

私は、全体のコードを書いていますエラーと言います! – safarov

+0

PHPコードを交換してくださいと同じエラーが再び表示され、再び – safarov

関連する問題