2013-02-21 50 views
5

मैं किसी सर्वर को एक php स्क्रिप्ट पर अनुरोध करने का प्रयास कर रहा हूं जो यह देखने के लिए जांच करेगा कि कोई उपयोगकर्ता डेटाबेस में मौजूद है या नहीं। वर्तमान में मैं बस यह सुनिश्चित करना चाहता हूं कि मुझे कुछ प्रकार की प्रतिक्रिया मिल रही है। मैं responseString के मान को आउटपुट करने का प्रयास करता हूं जब उपयोगकर्ता लॉगिन बटन दबाता है लेकिन हर बार यह null के रूप में वापस आता है। क्या किसी को पता है क्यों ?? फिलहाल मैं मुश्किल कोड जोड़ने के बाद विवरण मैं DB में अस्तित्व के लिए जानते हैं और बस के लिए एक प्रतिक्रिया वापस क्या प्राप्त करना चाहते हैं -HTTP पोस्ट अनुरोध से नल प्रतिक्रिया

यह मेरा 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; 
} 

यह PHP स्क्रिप्ट मैं का अनुरोध कर रहा हूँ है उपयोगकर्ता मौजूद है कहते हैं।

<?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' कि प्रदान कर सकता हूँ' out.close कॉल करने से पहले () '?? मुझे बताएं कि क्या वह स्ट्रिंग 'शून्य' – Geros

+0

@ system32 है जैसा आपने सुझाव दिया था, यह अभी भी 'शून्य 'का मान देता है। ऐप कभी क्रैश नहीं हो रहा था, यह हमेशा 'null' को' प्रतिक्रिया स्ट्रिंग 'के मान के रूप में वापस कर रहा था। – Javacadabra

+0

'e.printStackTrace() 'को अपने' पकड़ 'ब्लॉक पर रखें और स्टैकट्रैक पोस्ट करें। यह मदद कर सकता है। – Geros

उत्तर

8

यह शून्य है क्योंकि आप कोड को असीमित रूप से निष्पादित करते हैं। आप परिणाम को टोस्ट करते हैं जबकि HTTP अनुरोध अभी तक आपकी PHP स्क्रिप्ट निष्पादित नहीं किया गया है।

अपने टोस्ट को पर पोस्टस्टेक्सक्यूट (स्ट्रिंग परिणाम) विधि को अपने AsyncTask क्लास में डालने का प्रयास करें।

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

धन्यवाद, यह काम किया! – Javacadabra

0

आप respose से कुछ नहीं मिल रहा है, लेकिन यह में डाल ... उत्पादन स्ट्रीम के बजाय ByteArrayInputStream का उपयोग .....