2012-08-17 8 views
0

안녕하세요, mysql 데이터베이스에서 사용자 이름과 암호를 확인하려고합니다. 여기서 md5() 해시를 사용하여 암호를 입력 했으므로 암호를이 사용자 이름에 액세스하고 싶습니다. 내가org.json.jsonexception 문자 1의 입력 끝이

오류 "의 문자 하나에 입력 org.json.jsonexception 끝"

을 얻고있다.

이것은 내 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(); 
} 
?> 

안드로이드 코드

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

+0

로깅을 고려 했습니까? –

+0

내 안드로이드 코드가 잘못되었다고 생각합니다. 어떤 제안있어? – suresh