2013-02-21 12 views
5

私は、データベースにユーザが存在するかどうかを確認するPHPスクリプトをサーバに要求しようとしています。現在、私は何らかの応答を受けていることを確認したいだけです。ユーザーがログインボタンを押したときに、それがnullに戻るたびにresponseStringの値を出力しようとします。誰もがなぜ知っていますか?HTTP Post Requestからのヌル応答

これは、これは私が要求していますPHPスクリプトである私のMainActivity

public class MainActivity extends Activity { 

EditText username; 
EditText password; 
Button loginBtn; 
LinearLayout loginform; 
String passwordDetail; 
String usernameDetail; 
String url = "http://www.mysite.com/example/checklogin.php"; 

String responseString = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Hide the Action Bar 
    ActionBar ab; 
    ab = this.getActionBar(); 
    ab.hide(); 

    //Get references to XML 
    username = (EditText)findViewById(R.id.username); 
    password = (EditText)findViewById(R.id.password); 
    loginBtn = (Button)findViewById(R.id.loginBtn); 
    loginform = (LinearLayout)findViewById(R.id.loginform); 

    //Animation 
    final AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f); 
    AlphaAnimation fadeOut = new AlphaAnimation(1.0f , 0.0f) ; 
    fadeIn.setDuration(1200); 
    fadeIn.setFillAfter(true); 
    fadeOut.setDuration(1200); 
    fadeOut.setFillAfter(true); 
    fadeOut.setStartOffset(4200+fadeIn.getStartOffset()); 

    //Run thread after 2 seconds to start Animation 
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable(){ 

     public void run() { 
      //display login form 
      loginform.startAnimation(fadeIn); 
      loginBtn.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        //display(); 
        Toast.makeText(getApplicationContext(), "Checking login details...", Toast.LENGTH_SHORT).show(); 
        if(checkLoginDetails()){ 
         //OPENS NEW ACTIVITY 
         //Close splash screen 
         //finish(); 
         //start home screen 
         Intent intent = new Intent(v.getContext(), SectionsActivity.class); 
         //startActivity(intent); 
         //creates fade in animation between two activities 
         overridePendingTransition(R.anim.fade_in, R.anim.splash_fade_out); 
         Toast.makeText(getApplicationContext(), "Login Successful" + responseString, Toast.LENGTH_SHORT).show(); 

        } 
        else{ 
         Toast.makeText(getApplicationContext(), "Login Unsuccessful", Toast.LENGTH_SHORT).show(); 

        } 
       } 
      }); 

     } 

    }, 2000); 
} 

//Check the login details before proceeding. 
public boolean checkLoginDetails(){ 
    usernameDetail = username.getText().toString(); 
    passwordDetail = password.getText().toString(); 
    new RequestTask().execute(url, usernameDetail, passwordDetail); 
    return true; 
} 

です - 現時点では私はハード私がDBに存在することを知っているだけに応答を取り戻すに集中したい内容をコード化しましたユーザーが存在するとします。

<?php 
mysql_connect("xxx.xxx.xxx.xxx", "username", "password") or die("Couldn't select database."); 
mysql_select_db("databasename") or die("Couldn't select database."); 

//$username = $_POST['username']; 
//$password = $_POST['password']; 

$pwdMD5 = md5(123); 

$sql = "SELECT * FROM membership WHERE Username = 'user1' AND Password = '$pwdMD5' "; 
$result = mysql_query($sql) or die(mysql_error()); 
$numrows = mysql_num_rows($result); 
if($numrows > 0) 
    { 
    echo 'user found'; 
    return true; 
    } 
else 
    { 
    echo 'user not found'; 
    return false; 

} 
    ?> 

これは私のAsyncTaskです。

class RequestTask extends AsyncTask<String, String, String>{ 

    @Override 
    protected String doInBackground(String... uri) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response; 
     responseString = null; 
     try { 
      response = httpclient.execute(new HttpPost(uri[0])); 
      StatusLine statusLine = response.getStatusLine(); 
      if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response.getEntity().writeTo(out); 
       out.close(); 
       responseString = out.toString(); 
      } else{ 
       //Closes the connection. 
       response.getEntity().getContent().close(); 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch (ClientProtocolException e) { 
      //TODO Handle problems.. 
     } catch (IOException e) { 
      //TODO Handle problems.. 
     } 
     return responseString; 
    } 
+0

はあなたがresponseString''ことを割り当てることはでき.....代わりに、出力ストリームのするByteArrayInputStreamを使用して... resposeから何かを得るが、それには入れていません) '??その文字列がヌルであるかどうかを知らせてください。 – Geros

+0

@ system32提案したとおりにしましたが、それでも 'null 'の値を返します。アプリケーションがクラッシュすることはなく、 'responseString'の値として常に' null'を返すだけでした。 – Javacadabra

+0

'e.printStackTrace()'を 'catch'ブロックに置き、スタックトレースをポストします。それは助けるかもしれない。 – Geros

答えて

8

コードを非同期で実行するため、nullです。 HTTPリクエストがPHPスクリプトの実行をまだ完了していない間に結果をトーストします。

AsyncTaskクラスでトーストをonPostExecute(文字列の結果)メソッドに入れてみてください。

@Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     //Toast result. 
    } 
+0

ありがとう、それは仕事をした! – Javacadabra

0

あなたは( `out.closeを呼び出す前に

関連する問題