Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将post数据发送到php不工作_Android - Fatal编程技术网

Android 将post数据发送到php不工作

Android 将post数据发送到php不工作,android,Android,我有个问题要发送 同时向服务器发送电子邮件、姓名和电话 有解决办法吗 我工作的地方在底部 public class MainActivity extends Activity { EditText name; EditText email; EditText tlf; Button btn; string resultat=null; InputStream is = null; StringBuilder sb = null ; @Override protected void onCre

我有个问题要发送 同时向服务器发送电子邮件、姓名和电话

有解决办法吗 我工作的地方在底部

public class MainActivity extends Activity {

EditText name;
EditText email;
EditText tlf;
Button btn;
string resultat=null;
InputStream is = null;
StringBuilder sb = null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);
    name= (EditText)findViewById(R.id.name);
    email = (EditText)findViewById(R.id.email);
    tlf = (EditText)findViewById(R.id.tlf);
    btn = (Button)findViewById(R.id.btn);
}
public void envoyerMessage (View v){
    HttpClient client= new DefaultHttpClient();
    HttpPost post=new HttpPost("http://xxxxcom/app.php");
    String  nam = name.getText().toString();
    String  mail = email.getText().toString();
    String  fon = tlf.getText().toString();
    if(log.length() >0){
        try {
            List<NameValuePair> donnees = new ArrayList<NameValuePair>(1);
            donnees.add(new BasicNameValuePair("id", nam ));
            donnees.add(new BasicNameValuePair("mal", mail ));
            donnees.add(new BasicNameValuePair("fn", fon ));
            post.setEntity(new UrlEncodedFormEntity(donnees));
            client.execute(post);
            login.setText("");
            Toast.makeText(this, "valid login", Toast.LENGTH_SHORT).show();
公共类MainActivity扩展活动{
编辑文本名称;
编辑文本电子邮件;
编辑文本tlf;
按钮btn;
字符串result=null;
InputStream=null;
StringBuilder sb=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(右布局、主布局);
name=(EditText)findViewById(R.id.name);
email=(EditText)findviewbyd(R.id.email);
tlf=(EditText)findViewById(R.id.tlf);
btn=(按钮)findViewById(R.id.btn);
}
公使(视图五){
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(“http://xxxxcom/app.php");
字符串nam=name.getText().toString();
字符串mail=email.getText().toString();
字符串fon=tlf.getText().toString();
如果(log.length()>0){
试一试{
List donnees=新数组列表(1);
添加(新的BasicNameValuePair(“id”,nam));
添加(新的BasicNameValuePair(“mal”,邮件));
添加(新的BasicNameValuePair(“fn”,fon));
post.setEntity(新的UrlEncodedFormEntity(donnees));
客户。执行(post);
login.setText(“”);
Toast.makeText(这是“有效登录”,Toast.LENGTH_SHORT).show();

谢谢

我的建议是使用截击,这里是一个非常小的示例:

射门得分

但是你可以试试这个:

public static class Connector extends AsyncTask<String, Object, Object> {
    private static final int DEFAULT_CNN_TIME_OUT = 1000 * 20;
    private static String    USER_AGENT;
    private String           mUrl;
    private byte[]           mBody;

    public Connector( Context _cxt ) {
        super();
        if( TextUtils.isEmpty( USER_AGENT ) ) {
            // Without this, the default provided by API will be like "Dalvik/1.6.0 (Linux; U; Android 4.0.4; BASE_Lutea_3 Build/IMM76D)" .
            USER_AGENT = new WebView( _cxt ).getSettings().getUserAgentString();
        }
    }

    /*
     * Convenient function to execute the connecting task.
     */
    public void submit( String _url ) {
        this.execute( _url );
    }

    @Override
    protected Object doInBackground( String... _params ) {
        mUrl = _params[0];
        Object ret = null;
        HttpURLConnection conn = null;
        try {
            synchronized( Connector.class ) {
                InputStream in = null;
                conn = connect( mUrl );
                // if we don't do conn.setRequestProperty( "Accept-Encoding", "gzip" ), the wrapper GZIPInputStream can be removed.
                onConnectorInputStream( in = new GZIPInputStream( conn.getInputStream() ) );
                in.close();
                in = null;
            }
        }
        catch( Exception _e ) {
            ret = _e;
        }
        finally {
            if( conn != null ) {
                conn.disconnect();
                conn = null;
            }
        }
        return ret;
    }

    @Override
    protected void onPostExecute( Object _result ) {
        if( _result instanceof SocketTimeoutException ) {
            onConnectorConnectTimout();
        } else if( _result instanceof ConnectorPostConnectException ) {
            onConnectorError( ((ConnectorPostConnectException) _result).getStatus() );
        } else if( _result instanceof Exception ) {
            onConnectorInvalidConnect( (Exception) _result );
        } else if( _result == null ) {
            onConnectorFinished();
        }
        handleEstablishedConnection();
    }

    /*
     * Internal help and test function.
     */
    private static void handleEstablishedConnection() {
        try {
            Log.v( TAG, "Connection is established." );
            CookieStore cs = CookieManager.getInstance().getCookieStore();
            if( cs != null ) {
                Log.v( TAG, "------------cookies------------" );
                List<Cookie> list = cs.getCookies();
                if( list != null && list.size() > 0 ) {
                    StringBuilder cookieBuilder = new StringBuilder();
                    for( Cookie c : list ) {
                        cookieBuilder
                                .append( c.getName().trim() )
                                .append( "=>" )
                                .append( c.getValue().trim() )
                                .append( "=>" )
                                .append( c.getDomain() );
                        Log.v( TAG, cookieBuilder.toString() );
                        cookieBuilder.delete( 0, cookieBuilder.length() - 1 );
                    }
                    cookieBuilder = null;
                } else {
                    Log.v( TAG, "Empty cookies." );
                }
                cs = null;
                list = null;
            }
        }
        catch( Exception _e ) {
            Log.e( TAG, "Error in handleEstablishedConnection: " + _e.getMessage() );
        }
        finally {
        }
    }

    private HttpURLConnection connect( String _urlStr ) throws Exception {
        URL url = null;
        HttpURLConnection conn = null;
        try {
            try {
                url = new URL( _urlStr );
            }
            catch( MalformedURLException e ) {
                throw new IllegalArgumentException( "Invalid url: " + _urlStr );
            }
            conn = preConnect( url );
            doConnect( conn );
            conn = postConnect( conn );
        }
        catch( Exception _e ) {
            throw _e;
        }
        finally {
            url = null;
        }
        return conn;
    }

    private HttpURLConnection preConnect( URL url ) throws Exception {
        HttpURLConnection conn;
        conn = (HttpURLConnection) url.openConnection();
        conn.setUseCaches( false );
        // http://www.aswinanand.com/2009/01/httpurlconnectionsetfollowredirects-bug/comment-page-1/#comment-13330
        // see the url to learn more about the problem of redirect
        conn.setInstanceFollowRedirects( false );
        conn.setDoOutput( true );// allows body
        mBody = getBody();
        if( hasBody() ) {
            conn.setFixedLengthStreamingMode( mBody.length );
        }
        conn.setRequestMethod( "POST" );
        conn.setRequestProperty( "Connection", "Keep-Alive" );
        conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8" );
        conn.setRequestProperty( "User-Agent", USER_AGENT );
        conn.setRequestProperty( "Accept-Encoding", "gzip" );// force server to send in Content-Encoding: gzip .
        String cookies = onCookie();
        if( !TextUtils.isEmpty( cookies ) ) {
            conn.setRequestProperty( "Cookie", onCookie() );
            cookies = null;
        }
        conn.setConnectTimeout( onSetConnectTimeout() );
        return conn;
    }

    /*
     * Convenient function to check the exiting of body.
     */
    private boolean hasBody() {
        return mBody != null && mBody.length > 0;
    }

    private void doConnect( HttpURLConnection conn ) throws Exception {
        OutputStream out; // the outgoing stream
        out = conn.getOutputStream();
        if( hasBody() ) {
            out.write( mBody );
        }
        out.close();
        out = null;
    }

    private HttpURLConnection postConnect( HttpURLConnection conn ) throws Exception {
        int status = conn.getResponseCode();
        if( status != HttpURLConnection.HTTP_OK ) {
            throw new ConnectorPostConnectException( status );
        } else {
            CookieManager.getInstance().put( conn.getURL().toURI(), conn.getHeaderFields() );
            return conn;
        }
    }

    private byte[] getBody() {
        byte[] body = null;
        String bodyString = onSetBody();
        if( !TextUtils.isEmpty( bodyString ) ) {
            body = bodyString.getBytes();
        }
        return body;
    }

    // ------------------------------------------------
    // Overrides methods here
    // ------------------------------------------------

    protected int onSetConnectTimeout() {
        return DEFAULT_CNN_TIME_OUT;
    }

    protected String onCookie() {
        return null;
    }

    protected String onSetBody() {
        return null;
    }

    protected void onConnectorConnectTimout() {
        Log.e( TAG, "Handling connector timeout gracefully." );
    }

    protected void onConnectorError( int _status ) {
        Log.e( TAG, "Handling connector error(responsed) gracefully: " + _status );
    }

    protected void onConnectorInvalidConnect( Exception _e ) {
        Log.e( TAG, "Handling connector invalid connect(crash) gracefully: " + _e.toString() );
    }

    /*
     * Read data here. The function runs in thread. To hook on UI thread use onConnectorFinished()
     */
    protected void onConnectorInputStream( InputStream _in ) {
    }

    /*
     * Last handler for a success connection
     */
    protected void onConnectorFinished() {
    }
}

您有什么问题?您有任何logcat输出吗?如果有,请将其添加到您的问题中。
mConn = new Util.Connector( getApplicationContext() ) {
            @Override
            protected String onSetBody() {
                String body = null;
                try {
                    StringBuilder bodyBuilder = new StringBuilder();
                    bodyBuilder
                            .append( "id=" )//.append(...)
                            .append( '&' )
                            .append( "mal=" )//.append(...) 
                    body = bodyBuilder.toString();
                    bodyBuilder = null;
                }
                catch( Exception _e ) {
                    Log.e( TAG, "Error in onCreateBody: " + _e.getMessage() );
                }
                finally {
                }
                return body;
            }



            @Override
            protected int onSetConnectTimeout() {
                return 10 * 1000;
            }

            @Override
            protected void onConnectorInvalidConnect( Exception _e ) {
                super.onConnectorInvalidConnect( _e );
                onErr();
            }

            @Override
            protected void onConnectorError( int _status ) {
                super.onConnectorError( _status );
                onErr();
            }

            protected void onConnectorConnectTimout() {
                super.onConnectorConnectTimout();
                onErr();
            }

            private void onErr() { 
                //mConn.submit( URL );
            };

            @Override
            protected void onConnectorFinished() {

            }

            @Override
            protected void onConnectorInputStream( InputStream _in ) {

            }
        };
        mConn.submit( URL );