0

I am new to Android and trying to post data to localhost from my Android App. While it is not showing any errors, it's just displaying 0<br>0 in Android code. But in PHP code it's displayed "empty"
My php code is:

<?php
            if (empty($_POST['name']) || empty($_POST['pass']))
        {
            echo "empty";
        }
        else{
            $name = $_POST['name'];
            $pass = $_POST['pass'];
            echo "Name:" + $name;
            echo "<br>";
            echo "Pass:" + $pass;
        }
?>

I'm just sending username and password from an Android App to print in an PHP page. My Android code is:

    package com.example.connecttophp;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText usn = (EditText)findViewById(R.id.editText1);
    final EditText pwd = (EditText)findViewById(R.id.editText2);
    final TextView logme = (TextView)findViewById(R.id.textView3);
    Button clear = (Button)findViewById(R.id.button2);
    clear.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            usn.setText("");
            pwd.setText("");
            logme.setText("");
        }
    });
    Button send = (Button)findViewById(R.id.button1);
    send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new SendDatatoPhp(logme).execute(usn.getText().toString(),pwd.getText().toString());
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
   }

   class SendDatatoPhp extends AsyncTask<String,String,String>{

TextView logme;
public SendDatatoPhp(TextView et){
    logme = et;
}

@Override
protected String doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/demo.php");
    String responseBody;
    try{
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("name",arg0[0]));
    nameValuePairs.add(new BasicNameValuePair("pass",arg0[1]));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    responseBody = EntityUtils.toString(response.getEntity());
    return responseBody;
    }
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } 
    catch (IOException e) {
        // TODO Auto-generated catch block
    }
    return "Login Not Successful";
}
protected void onPostExecute(String response){
    logme.setText(response);
      }
    }   

My XML is as follows

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="28dp"
    android:text="Enter Username:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:ems="10" >

</EditText>


<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="36dp"
    android:text="Enter Password"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignRight="@+id/editText1"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="33dp"
    android:ems="10"
    android:inputType="textPassword" >
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="37dp"
    android:text="Send" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignRight="@+id/editText1"
    android:layout_marginRight="28dp"
    android:text="Clear" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView2"
    android:layout_below="@+id/button1"
    android:layout_marginTop="62dp"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall" />

   </RelativeLayout>

I have declared permission in mainfest file.

I don't know what the error is.

I want the PHP file to receive username and password and print it.

How can I do this ?

twlkyao
  • 14,302
  • 7
  • 27
  • 44
  • That would mean it's entering the `else` statement. Have you verified on the android side what values are your sending? – nKn Mar 18 '14 at 09:02
  • so as u can see in code I'm sending username as `name` and password as `pass`. – user3432226 Mar 18 '14 at 09:06
  • I mean you should debug the values being sent, for instance, put a `Log.d("dbg", "My username is " + arg0[0] + " and my password is " + arg0[1]);` line in your code and see whether what you're getting in the server side is what you are sending. – nKn Mar 18 '14 at 09:08
  • K i will try it out and come back. – user3432226 Mar 18 '14 at 09:10
  • I checked in log, its showing what i have typed in edittext. Suppose if I type `shilpa` as username and password as `pass` its showing the same. – user3432226 Mar 18 '14 at 09:31
  • Is there a problem within java code?? or in php?? – user3432226 Mar 18 '14 at 09:33
  • It should work in both ways, assuming that what you have tried showing is your `arg0[]` variables. If you're seeing `0
    0` it's due to my answer below.
    – nKn Mar 18 '14 at 09:35
  • Ya ur explainition is rite but how to check why `0` is going instead of `usrname` and `password`. – user3432226 Mar 18 '14 at 09:40
  • There must be something additional in your code, as the piece of code you've posted looks good to me... – nKn Mar 18 '14 at 11:15

0 Answers0