从android到php的Http url连接不起作用

时间:2016-03-04 18:36:05

标签: android android-intent

我需要一个代码,将数据从android发送到php(apache web服务器),假设我有一个EditText供用户输入消息和按钮,点击按钮我希望将消息发送到服务器,我希望它使用http url连接,因为我的Android版本是23.0.2。这是我试过的代码:     包com.example.user.test7;

 import android.net.Uri;
  import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.view.View;
 import android.view.Menu;
 import android.view.MenuItem;
  import android.widget.Button;
  import android.widget.EditText;

 import java.io.IOException;
  import java.io.OutputStreamWriter;
   import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
   import java.net.ProtocolException;
       import java.net.URL;

      public class MainActivity extends AppCompatActivity {

    private EditText Message;
   private Button Save;
       private String Msg;

@Override
protected void onCreate(Bundle savedInstanceState) {

    Message = (EditText) findViewById(R.id.msg);
    Save = (Button) findViewById(R.id.button);
    Msg = Message.getText().toString();

    Save.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    urlConnect();
                                }
                            }

    );
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton)                             

      findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

private void urlConnect(){
    HttpURLConnection connection = null;

    URL url = null;
    try {
        url = new URL("http://192.168.0.21/test7.php");
        connection = (HttpURLConnection)url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);
        connection.setConnectTimeout(720000000);
        connection.setReadTimeout(72000000);
        connection.connect();

        Uri.Builder builder = new Uri.Builder().appendQueryParameter("message", Msg);
        String data = builder.build().getEncodedQuery();

        OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
        output.write(data);
        output.flush();
        output.close();

        connection.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:-1)

您可以将EditText消息传递给URL并执行这样的Web服务 -

HttpURLConnection connection = null;

URL url = new URL("http://your_url/test.php");

connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setConnectTimeout(720000000);
connection.setReadTimeout(72000000);
connection.connect();

Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("message", message)
String data = builder.build().getEncodedQuery();

OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
 output.write(data);
 output.flush();
 output.close();

connection.disconnect();

在此基本上,您将附加所有输入数据并形成查询。

希望这会有所帮助!!