2012-08-17 121 views
0

您好我正在尝试从mysql数据库中检查用户名和密码,其中我使用md5()哈希插入我的密码现在我想访问此用户名,密码。我得到org.json.jsonexception输入字符1输入结束

“输入org.json.jsonexception结束在性格1”

错误。

这是我的php代码。

<?php 
error_reporting(E_ALL^E_NOTICE^E_WARNING); 
$dbhost="localhost"; 
$dbuser="dev"; 
$dbpass="dev"; 
$dbdb="myandroid"; 
$connect =mysql_connect($dbhost,$dbuser,$dbpass) or die("connection error"); 
mysql_select_db($dbdb) or die("database selection error"); 
$username=$_POST["username"]; 
$password=$_POST["password"]; 
$pass=md5('$password'); 
$query=mysql_query("SELECT * FROM androidtable WHERE username='$username' AND password='$pass'")or die(mysql_error()); 

$num=mysql_num_rows($query); 
if($num==1){ 
while($list=mysql_fetch_assoc($query)){ 
$output=$list; 
echo json_encode($output); 
} 
mysql_close(); 
} 
?> 

Android的代码

public class DBActivity extends Activity implements OnClickListener{ 
EditText eduser, edpass; 
Button logbutton; 

String username, password; 

HttpClient httpclient; 

HttpPost httppost; 

ArrayList<NameValuePair> nameValuePair; 

HttpResponse httpresponse; 
HttpEntity httpentity; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_db); 
    initialise(); 


} 

private void initialise() { 
    eduser=(EditText) findViewById(R.id.eduser); 
    edpass=(EditText) findViewById(R.id.edpass); 
    logbutton=(Button) findViewById(R.id.login); 
    logbutton.setOnClickListener(this); 

} 

public void onClick(View v) { 
    httpclient=new DefaultHttpClient(); 

    httppost=new HttpPost("http://192.168.1.2/androidtut/check.php"); 
    username=eduser.getText().toString(); 
    password=edpass.getText().toString(); 



    try{ 
     nameValuePair=new ArrayList<NameValuePair>(); 

     nameValuePair.add(new BasicNameValuePair("username", username)); 
     nameValuePair.add(new BasicNameValuePair("password", password)); 

     httppost.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
     httpresponse=httpclient.execute(httppost); 
     if(httpresponse.getStatusLine().getStatusCode()==200){ 
      httpentity=httpresponse.getEntity(); 
      if(httpentity != null){ 
       InputStream is=httpentity.getContent(); 
       JSONObject jresp=new JSONObject(convertStreamToString(is)); 

       String retUser=jresp.getString("username"); 
       String retPass=jresp.getString("password"); 

       if(username.equals(retUser) && password.equals(retPass)){ 

        SharedPreferences sp=getSharedPreferences("tableandroid",0); 
        SharedPreferences.Editor spedit=sp.edit(); 
        spedit.putString("username", username); 
        spedit.putString("password", password); 
        spedit.commit(); 

        Toast.makeText(getBaseContext(), "loggin success",Toast.LENGTH_LONG).show(); 
       }else{ 
        Toast.makeText(getBaseContext(), "user invalid",Toast.LENGTH_LONG).show(); 
       } 

      } 
     } 

    }catch(Exception e){ 
     String error=e.toString(); 
     Toast.makeText(getBaseContext(), error,Toast.LENGTH_LONG).show(); 
    } 


} 

private static String convertStreamToString(InputStream is) { 
    /* 
    * To convert the InputStream to String we use the BufferedReader.readLine() 
    * method. We iterate until the BufferedReader return null which means 
    * there's no more data to read. Each line will appended to a StringBuilder 
    * and returned as String. 
    */ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

}

回答

0

您的服务器不返回任何东西。所以你的Android客户端没有什么可以组织的。

$pass=md5('$password'); 

是不是散列密码,但$password字符串文字,而不是(单引号不扩展变量)。改为使用以下代码:

$pass=md5($password); 
+0

仍然出现错误。现在错误是用户无效的。但用户名和密码正确\ – suresh 2012-08-17 13:33:44

+0

您是否考虑过登录? – 2012-08-17 14:26:54

+0

我认为我的android代码是错误的。你有什么建议吗? – suresh 2012-08-17 14:35:21

相关问题