gpt4 book ai didi

java - 将数据从 Android 应用发布到 PHP 页面

转载 作者:行者123 更新时间:2023-12-01 05:31:29 26 4
gpt4 key购买 nike

我一直在查看其他几篇文章,并尝试组合一种方法将数据从我的 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();
}

}

目前,当我单击按钮进行检查(运行此 Activity )时,我收到 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

修改了代码,代码现在位于绘制输入文本的 View 的 Activity 中,而不是单独的 Activity 中:

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();
}
}

}

最佳答案

我在您的 Activity 中没有看到 setContentView() 方法。因此,当您尝试获取长度时,domainText 变量为 null

关于java - 将数据从 Android 应用发布到 PHP 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8908842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com