I've been looking at several other posts and trying to put together a way to POST data from my android app to a php page on our website. Basically it's a domain check function and I want the user to be able to type in a domain name within the app then when they click "Check Now" they get taken to the web page and it displays the results. I'm not worried about displaying the results within the app at the moment.
This is the code I have so far:
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();
}
}
At the moment I get a nullPointerError when I click the button to check (which runs this Activity).
I'm still quite new to Java so the may be a considerably easier way to do this!
Thanks
Edit:
Error:
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
Revised code, code is now in the activity which draws the view where the text is inputted, rather than a separate 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();
}
}
}
I don't see the
setContentView()
method in your activity. So thedomainText
variable is null when you try to get the length