【问题标题】:Post data from Android to PHP script on localhost将数据从 Android 发布到本地主机上的 PHP 脚本
【发布时间】:2014-04-23 20:21:10
【问题描述】:

我是 Android 新手,正在尝试从我的 Android 应用程序将数据发布到 localhost。虽然它没有显示任何错误,但它只是在 Android 代码中显示 0<br>0。但在 PHP 代码中,它显示为 "empty"
我的php代码是:

<?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;
        }
?>

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

    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 文件接收用户名和密码并打印出来。

我该怎么做?

【问题讨论】:

  • 这意味着它正在进入else 语句。您是否在 android 端验证了您发送的值是什么?
  • 所以你可以在代码中看到我将用户名发送为name,密码发送为pass
  • 我的意思是你应该调试正在发送的值,例如,在你的代码中添加一个Log.d("dbg", "My username is " + arg0[0] + " and my password is " + arg0[1]); 行,看看你在服务器端得到的是否就是你正在发送的。跨度>
  • 我会尝试并回来。
  • 我签入了日志,它显示了我在edittext中输入的内容。假设如果我输入 shilpa 作为用户名和密码作为 pass 它显示相同。

标签: php android httpconnection


【解决方案1】:

检查你是否得到了这一行之后的值。

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

 

Log.e("uname",arg[0]);
Log.e("pass",arg[1]);

输入日志并检查您是否获得了价值..?

【讨论】:

  • 它显示正确,我在 EditText 中输入的用户名和密码。
  • 请在浏览器中使用 Postman(chrome add on) 检查您的网络服务。它工作正常吗..?
  • chrome.google.com/webstore/search/postman?hl=en-US 点击链接并安装 postman Launcher 插件。只需编写 url 设置方法 post 并将参数作为键值传递,如 android 并检查响应..:)
  • 是的,你的仪式。网络服务无法正常工作我尝试使用邮递员显示0&lt;br&gt;0,即使在传递了正确的值之后也是如此。那么如何让它现在工作呢??
  • 好吧,所以你有 php scriptur android 代码的问题很好。祝你好运
猜你喜欢
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
相关资源
最近更新 更多