• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Posting to a php webpage

RossTiger

Newbie
The situation is quite simple but I don't know how to do it with Java.

Basically I want to POST a domain name from a TextInput field in my app to a domainchecker.php page on a website.

So, when people enter the domain name in the app and hit the button, it takes them to our webpage and displays the results for the domain they chose.

Doesn't need to be in a WebView or anything, just having it open the browser will do fine.

Seems simple but I don't know my way around Java that well.
 
Thanks, getting there now :)

Could you (or someone) explain this bit to me:


// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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



All I want to send is the content of an EditText box.

Something along the lines of:
EditText domainText = (EditText) findViewById(R.id.hosting_link);
?? But then what to do
 
First, they create a List of type NameValuePair. In your case, you have to create one pair, where the name is equal to the fieldname in your PHP script. As the value, fill in the value in the TextInput field of your Android app.
 
Something like this:

EditText domainText = (EditText) findViewById(R.id.hosting_link);

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


Think I've grabbed the value from the EditText field at the start and put it into a variable called domainText. Then put domainText as the value, not sure how to word this part though as it throws up an error in eclipse.
 
Sorry, I'm a bit out of my depth with this, but determined to get it to work.

I think the BasicNameValuePair() constructor has to have two strings e.g.

BasicNameValuePair("id" "12345")

But I don't want to send the same thing every time, I want it to change, that why I wanted to use a variable.
 
Actually scratch that, think it's ok with the variable. But is says:

The constructor BasicNameValuePair(String, EditText) is undefined

And it wants to change the EditText at the top to String?
 
Solved that problem with this:

nameValuePairs.add(new BasicNameValuePair("id",domainText.getText().toString()));

Now it works...almost :P

When I click the button it just goes to a blank screen within the app.

This is the onClick code:

public void onClick (View thisView) {
switch (thisView.getId()) {
case R.id.domain_button:
Intent domCheck = new Intent(this, Domain_check.class);
startActivity(domCheck);
break;
 
I must apologiese for the constant posts. I keep figuring things out right after I post.

Solved the last problem because I hadn't actually told the Activity to RUN the public void postData().

Now at least I get an error, a nullpointerexception error.
 
Ah OK, now I see where you're going. Basically you've got some options here. Since you've posted an HTTP request, I think you can get the answer back somehow by getting data from the Intent. However, the answer is a bunch of HTML. Perhaps you can create a view to display this HTML.

In my opinion, it's much better to send your HTTP request to a special PHP page, which doesn't output HTML, just plain text, perhaps comma-separated. You then get back some strings, which you can display somehow on the screen, using standard Android GUI components.
 
I must apologiese for the constant posts. I keep figuring things out right after I post.

Solved the last problem because I hadn't actually told the Activity to RUN the public void postData().

Now at least I get an error, a nullpointerexception error.

That means you try to use a variable/object which hasn't been filled.

Also, I'd try to check the HTTP server logs, to see if the request actually arrives in your PHP script.
 
Ah OK, now I see where you're going. Basically you've got some options here. Since you've posted an HTTP request, I think you can get the answer back somehow by getting data from the Intent. However, the answer is a bunch of HTML. Perhaps you can create a view to display this HTML.

In my opinion, it's much better to send your HTTP request to a special PHP page, which doesn't output HTML, just plain text, perhaps comma-separated. You then get back some strings, which you can display somehow on the screen, using standard Android GUI components.

I did think of doing this, and if the app was just for domain checking I would, but all I want it to do is actually open the borwser(not in the app) on the php page and display the results from whatever they put in (in the app).

Edit: Definitely not getting to the server.
 
Back
Top Bottom