将数据从Android应用程序发布到PHP页面

时间:2012-01-18 10:51:13

标签: java php android postdata

我一直在寻找其他几个帖子,并试图将我的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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

我没有在您的活动中看到setContentView()方法。因此,当您尝试获取长度

时,domainText变量为null