1. Download our Official Android App: Forums for Android!

Posting to a php webpage

Discussion in 'Android Apps & Games' started by RossTiger, Jan 13, 2012.

  1. RossTiger

    RossTiger Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Jan 9, 2012

    Jan 9, 2012
    33
    0
    15
    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.
     

    Advertisement

  2. cerberusss

    cerberusss Member
    Rank:
    None
    Points:
    16
    Posts:
    56
    Joined:
    Sep 9, 2011

    Sep 9, 2011
    56
    4
    16
  3. RossTiger

    RossTiger Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Jan 9, 2012

    Jan 9, 2012
    33
    0
    15
    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
     
  4. cerberusss

    cerberusss Member
    Rank:
    None
    Points:
    16
    Posts:
    56
    Joined:
    Sep 9, 2011

    Sep 9, 2011
    56
    4
    16
    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.
     
  5. RossTiger

    RossTiger Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Jan 9, 2012

    Jan 9, 2012
    33
    0
    15
    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.
     
  6. cerberusss

    cerberusss Member
    Rank:
    None
    Points:
    16
    Posts:
    56
    Joined:
    Sep 9, 2011

    Sep 9, 2011
    56
    4
    16
    Googling for the error tells you what?
     
  7. RossTiger

    RossTiger Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Jan 9, 2012

    Jan 9, 2012
    33
    0
    15
    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.
     
  8. RossTiger

    RossTiger Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Jan 9, 2012

    Jan 9, 2012
    33
    0
    15
    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?
     
  9. RossTiger

    RossTiger Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Jan 9, 2012

    Jan 9, 2012
    33
    0
    15
    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;
     
  10. RossTiger

    RossTiger Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Jan 9, 2012

    Jan 9, 2012
    33
    0
    15
    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.
     
  11. cerberusss

    cerberusss Member
    Rank:
    None
    Points:
    16
    Posts:
    56
    Joined:
    Sep 9, 2011

    Sep 9, 2011
    56
    4
    16
    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.
     
  12. cerberusss

    cerberusss Member
    Rank:
    None
    Points:
    16
    Posts:
    56
    Joined:
    Sep 9, 2011

    Sep 9, 2011
    56
    4
    16
    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.
     
  13. RossTiger

    RossTiger Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    33
    Joined:
    Jan 9, 2012

    Jan 9, 2012
    33
    0
    15
    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.
     

Share This Page

Loading...