簡體   English   中英

將數據從Android應用發布到PHP頁面

[英]Posting Data from Android app to PHP page

我一直在看其他幾篇文章,並試圖提出一種將數據從我的android應用程序發布到我們網站上的php頁面的方法。 基本上,這是一個域檢查功能,我希望用戶能夠在應用程序中鍵入一個域名,然后當他們單擊“立即檢查”時,他們會進入網頁並顯示結果。 我現在不擔心在應用程序中顯示結果。

這是我到目前為止的代碼:

public class Domain_check extends Activity {

public void onCreate(Bundle savedInstanceState) {
    postData();
}

public void postData() {
    EditText domainText = (EditText) findViewById(R.id.hosting_link);
    if (domainText.length()>0)
    {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://www.oursite.com/domainchecker.php");



        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("id",domainText.getText().toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
    else
    {
        // display message if text fields are empty
        Toast.makeText(getBaseContext(),"Please enter a domain name",Toast.LENGTH_SHORT).show();
    }

} 

現在,當我單擊按鈕進行檢查時(運行此活動),我得到一個nullPointerError。

我對Java還是很陌生,因此這可能是一種更簡單的方法!

謝謝

編輯:

錯誤:

01-18 11:05:39.720: E/AndroidRuntime(353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oursite/com.oursite.Domain_check}: java.lang.NullPointerException

修改后的代碼,現在在活動中的代碼將繪制輸入文本的視圖,而不是單獨的活動:

public class Hosting extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hosting);

   View DomainButton = findViewById(R.id.domain_button);
   DomainButton.setOnClickListener((OnClickListener) this);     
}

public void onClick (View thisView) {
    postData();
}

public void postData() {
    EditText domainText = (EditText) findViewById(R.id.hosting_link);
    if (domainText.length()>0)
    {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://www.oursite.com/domainchecker.php");



        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("id",domainText.getText().toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
    else
    {
        // display message if text fields are empty
        Toast.makeText(getBaseContext(),"Please enter a domain name",Toast.LENGTH_SHORT).show();
    }
}

}

我在您的活動中看不到setContentView()方法。 因此,當您嘗試獲取長度時, domainText變量為null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM