通过服务器从手机加载短消息到计算机

时间:2016-07-18 15:21:35

标签: java php android server

如标题中所述,我希望通过服务器将短文本消息从手机发送到计算机。但我也是一个小应用程序,通过在服务器上传递日志文件编程我可以告诉服务器上的对象,但没有到达计算机。为什么会这样?

应用代码:

    public class MainActivity extends AppCompatActivity {
    EditText et;
    Button btn;
    TextView tv;

    final String scripturlstring = "http://firstsecond.esy.es/ArduinoControl/server_script.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        et = (EditText) findViewById(R.id.editText);
        tv = (TextView) findViewById(R.id.textView);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    sendToServer(et.getText().toString());
            }
        });
    }

    public void sendToServer(final String text) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String textparam = "text1=" + URLEncoder.encode(text, "UTF-8");

                    URL scripturl = new URL(scripturlstring);
                    HttpURLConnection connection = (HttpURLConnection) scripturl.openConnection();
                    connection.setDoOutput(true);
                    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    connection.setFixedLengthStreamingMode(textparam.getBytes().length);


                    OutputStreamWriter contentWriter = new OutputStreamWriter(connection.getOutputStream());
                    contentWriter.write(textparam);
                    contentWriter.flush();
                    contentWriter.close();

                    connection.disconnect();

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

PHP脚本:

    <?php> 
      $text = $_POST["text1"]; 
      ($text != null){ 
       echo("Empfangener Text: ".$text); 
       $logfile = fopen("logfile.txt", "a"); 
       fwrite($logfile, $text."\n"); 
       fclose($logfile); 
  } else{ 
       echo("No Command recieved!"); 
  } 
  ?> 

计算机上的Java代码:

    @SuppressWarnings("serial")
public class Launcher extends JFrame{
    final static String scripturlstring = "http://firstsecond.esy.es/ArduinoControl/server_script.php";
    static String inputLine;
    final static String text = "hallo";

    public Launcher() {
        setResizable(false);
        setPreferredSize(new Dimension(250, 210));
        pack();

        setTitle("ArduinoControl");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }
    public static void main(String[] args) throws Exception {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame launcher = new Launcher();
                launcher.setVisible(true);
            }

        });
        URL serverscripturl = new URL(scripturlstring);
        URLConnection yc = serverscripturl.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

在电脑上我只收到“没有收到命令!”。

感谢您的评论!!!

0 个答案:

没有答案