本帖最后由 杨兴庭 于 2013-7-28 22:53 编辑
- package com.example.test1;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import com.example.test1.R;
- import android.os.Bundle;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends Activity{
- String urlString = "http://127.0.0.1:8888/test/hello.php";
- private URL url;
- String line;
- String result = "";
- private HttpURLConnection httpUrlconnection;
- @Override
- protected void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- OnClickListener lis=new OnClickListener(){
- @SuppressLint("ShowToast")
- public void onClick(View v)
- {
- try {
- url = new URL(urlString);
- httpUrlconnection = (HttpURLConnection) url.openConnection();
- httpUrlconnection.setRequestMethod("POST");
- httpUrlconnection.setDoOutput(true);
- httpUrlconnection.setDoInput(true);
- httpUrlconnection.connect();
- PrintWriter out = new PrintWriter(httpUrlconnection.getOutputStream());
- out.print(R.id.EdiUser);
- out.flush();
- BufferedReader in = new BufferedReader(new InputStreamReader(httpUrlconnection.getInputStream()));
- while((line = in.readLine()) != null)
- {
- result += "\n"+line;
- }
- Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_LONG);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- };
- Button b=(Button)findViewById(R.id.btnSure);
- b.setOnClickListener(lis);
- }
- }
复制代码 |