2013-08-21 2 views
0

저는 Android 스크립트를 배우고 PHP 스크립트와 WAMP 서버를 사용하여 사용자 이름과 비밀번호를 확인하는 코드를 작성하려고합니다. 내 PHP 스크립트에서 정의되지 않은 인덱스 오류가 계속 발생합니다. 늘어나는만큼 내가 확인할 수있는 내 PHP 스크립트 URL에서 데이터를 액세스 할 수 없습니다. 다음은 관련 코드입니다. 어떤 도움을 주셔서 감사합니다. 여기 HTTP Post가 android에서 PHP와 작동하지 않습니다.

//build url data to be sent to server 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("username",username)); 
     nameValuePairs.add(new BasicNameValuePair("password",password)); 

     String result = ""; 
     InputStream is = null; 
     //http post 
     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2/PasswordCheck.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
     }catch(Exception e){ 
      Log.e("Connection", "Error in http connection "+e.toString()); 
     } 

내 PHP 스크립트

여기
<?php 
mysql_connect("localhost", "root", "") or die("could not connect to mysql"); 
mysql_select_db("drop-in") or die("database not found"); 

$username = $_POST["username"]; 
$suppliedPassword = $_POST["password"]; 
$databasePassword = ""; 
$output = "false"; 
$query = mysql_query("SELECT Password FROM users WHERE Username = '$username'") or die("query failed"); 
if(mysql_num_rows($query) > 0){ 
    $row = mysql_fetch_assoc($query); 
    $databasePassword = $row['password']; 
    if($databasePassword == $suppliedPassword) 
    { 
     $output = "true"; 
    } 
} 
    print($output); 
    mysql_close(); 
?> 

그리고 서버의 응답

http://imgur.com/sQStI2D

편집의 사진입니다 : 그래서 PHP 스크립트가 제공 되더라도 것을 알아 냈어 이 오류는 $ username과 $ password 변수에 my android 앱이 전달하려고 시도한 값을 포함합니다. 그러나 오류 테이블에 대한 HTML이 응답의 안드로이드 애플 리케이션에 다시 전송되기 때문에 이러한 오류의 존재는 여전히 혼란 스럽다.

답변

1

내게는 안드로이드 코드가 "username"을 "password"필드는 PHP 스크립트가 그들을 찾을 수없는 이유를 설명합니다. 코드, new ArrayList<NameValuePair>();에서

는 ArrayList에의 길이는이 코드 sample보고, 누락 될 수 있습니다 : 그것은 new ArrayList<NameValuePair>(2);을해야처럼, 당신이 그와 함께 시도해야하고 문제가 해결되는지 확인 보인다.

+0

ArrayList가 필요에 따라 증가하므로 문제가되지 않습니다. 그래도 고마워. –

0

간단한 맞춤법 오류였습니다. 대문자 대신에 '암호'라는 단어에 소문자 'p'를 사용했습니다. 이상하게도 그 오류가 발생했습니다.

0

당신은 PHP에서 json_decode() 함수를 시도 할 수 있습니다 :

시도를 서버에 파일을 만들고 배열 출력을 얻을 것이다 간단한 텍스트 파일로 안드로이드 요청의 내용을 넣어. 그 파일에 코드 아래 .PHP 파일 쓰기에서 서버에

:

<?php 
$data=file_put_content(collect.txt, $_POST); 
// if you got to know object or array name from txt file use it. 
$array=json_decode($data, true) // for array output. 
print_r($array); 
?> 

당신은 당신이 배열 이름을 알고있는 경우 직접 JSON 개체에서 사용할 수있는 파일에서 읽을 수 없다 경우

<?php 

$array=json_decode($data) // remove true for use as an object output. 
print_r($array); 
?> 

일단 배열을 가져 오면 새 변수에 값을 할당하고 원하는 값대로 수행하십시오. 데이터베이스 또는 아무것도 requiremnet 당 업데이트

관련 문제