私は他のいくつかの投稿を見て、Android アプリからウェブサイトの php ページにデータを POST する方法をまとめようとしました。基本的にはドメイン チェック機能であり、ユーザーがアプリ内でドメイン名を入力できるようにしてから、[今すぐ確認] をクリックすると Web ページに移動し、結果が表示されるようにします。現時点では、アプリ内で結果を表示する心配はありません。
これは私がこれまでに持っているコードです:
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();
}
}
}