将数据从Android发布到本地主机上的PHP脚本

将数据从Android发布到本地主机上的PHP脚本,php,android,httpconnection,Php,Android,Httpconnection,我是Android新手,正在尝试从Android应用程序向localhost发布数据。虽然它没有显示任何错误,但它只是在Android代码中显示00。但在PHP代码中,它显示为空 我的php代码是: 我只是从Android应用程序发送用户名和密码,以便在PHP页面中打印。我的Android代码是: package com.example.connecttophp; import java.io.IOException; import java.util.ArrayLis

我是Android新手,正在尝试从Android应用程序向localhost发布数据。虽然它没有显示任何错误,但它只是在Android代码中显示00。但在PHP代码中,它显示为空 我的php代码是:

我只是从Android应用程序发送用户名和密码,以便在PHP页面中打印。我的Android代码是:

    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);
      }
    }   
我的XML如下

   <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>
我已在mainfest文件中声明了权限

我不知道是什么错误

我希望PHP文件接收用户名和密码并打印出来


我怎样才能做到这一点?

检查这一行后面是否有值

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

放入日志并检查是否得到值?

这意味着它正在输入else语句。你在android端验证过你发送的是什么值吗?正如你在代码中看到的,我将用户名作为名称发送,密码作为密码发送。我的意思是你应该调试发送的值,例如,放置一个Log.ddbg,我的用户名是+arg0[0]+,密码是+arg0[1];输入代码,看看你在服务器端得到的是否是你正在发送的。K我会尝试一下,然后回来。我签入了日志,它显示了我在edittext中键入的内容。假设我输入shilpa作为用户名,密码作为pass,则显示相同。它正确显示了我在EditText中输入的用户名和密码。请在浏览器中使用PostAnchrome加载项检查您的web服务。它工作正常吗。。?单击链接并安装postman Launcher附加组件。只需编写url设置方法post,并像android一样将参数作为键值传递,然后检查响应..:是。webservice工作不正常,我试过让postman显示00,即使传递了正确的值。那么现在如何让它工作呢?好吧,只要你对php脚本有问题,android代码就可以了。祝你好运
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
Log.e("uname",arg[0]);
Log.e("pass",arg[1]);