Android表单到php, php文件不发送字段在Android表单中输入


android form to php, php file not sending fields inputted in android form

我开始学习android编程,对php完全是新手。

在发布这个问题之前,我已经尝试了所有与它相关的答案,没有一个适合我,所以我决定发布一个新的问题。

我有一个应用程序,用户在其中输入一个表单,它的内容通过电子邮件发送给我。

我成功地收到了电子邮件,但是只有php中定义的字符串。我也希望从用户的输入也得到交付到我的电子邮件。

请检查下面的php代码和java代码,并提出合适的建议。

Email Received:

"名:姓:你的电子邮件:你的电话:"

不需要用户以android形式输入。(

email2.php

<?php
$ed1=$_POST['fname'];
$ed2=$_POST['lname'];
$ed3=$_POST['email'];
$ed4=$_POST['phone'];
$to="ab5593@gmail.com";
$subject="android_email";
$from="$ed3";
$body="First Name:" .$ed1."'n Last Name:" .$ed2. "'n Your Email:".$ed3."'n Your Phone:"     .$ed4."'n";
$header="From :".$from."'n";
if(mail($to,$subject,$body,$header))
{
    echo "Thank you";
}
else
{
    echo"Try Again";
}
?>

FormActivity.java

package com.example.getpost;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
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.ByteArrayBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FormActivity extends Activity {
    TextView txtvw;
    String text;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form);
        txtvw = (TextView)findViewById(R.id.textView1);
        text = "";
        postData();
    }
    public void postData(){  
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.amit.comeze.com/email2.php");  
        EditText ed1 = (EditText)findViewById(R.id.editText1);
        EditText ed2 = (EditText)findViewById(R.id.editText2);
        EditText ed3 = (EditText)findViewById(R.id.editText3);
        EditText ed4 = (EditText)findViewById(R.id.editText4);
        try {
            // Add your data
            List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("fname", ed1.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("lname", ed2.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("email", ed3.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("phone", ed4.getText().toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            InputStream is = response.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(20);
            int current = 0;
            while((current = bis.read()) != -1){
                baf.append((byte)current);
            }  
            /* Convert the Bytes read to a String. */
            text = new String(baf.toByteArray());
            txtvw.setText(text);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
}

同样,当表单在android中打开时,"ThankYou"消息在启动时显示,即使表单是空的,我猜邮件也没有发送。

我需要的感谢信息出现后,用户按下提交按钮的形式,也所有的验证错误(不编码在php代码给出)出现后,用户按下提交按钮。

期待您的宝贵建议,这将对我有很大的帮助,我将不胜感激。
问候。

你的代码有很多问题:

首先,你不能在主线程上做网络操作。这里已经说过几千次了,要做到这一点,你需要使用AsyncTask。正如我所看到的,您按照本教程创建了帖子请求。在AsyncTask的doInBackground中复制完全相同的代码。

第二,你把你的post数据函数onCreate,这是一个函数调用时,活动开始,之前用户可以与您的应用程序交互,这就是为什么它发送一个空表单到您的php代码。为了解决这个问题,你应该只在onCreate上初始化你的视图。

public void postData(){   
EditText ed1 = (EditText)findViewById(R.id.editText1);
EditText ed2 = (EditText)findViewById(R.id.editText2);
EditText ed3 = (EditText)findViewById(R.id.editText3);
EditText ed4 = (EditText)findViewById(R.id.editText4);
}

也许可以放一个按钮来发送所有这些信息调用asyncTask的execute方法(参见上面的链接来理解asyncTask是如何工作的)。

我想在那之后你就可以让你的"应用"工作了!

希望能有所帮助。