所以我需要将消息从android stuio应用程序发送到python。我在网上浏览了所有内容,发现这是行不通的:(
如果可以的话,请看一下我的代码并向我解释如何解决它,我将很高兴! (服务器中包含sql,因此,对于所有其他混乱情况,我感到抱歉
服务器端(python):
import sqlite3
import socket
import sys
HOST = '0.0.0.0'
PORT = 5000
conn = sqlite3.connect('try.db')
c = conn.cursor()
try:
c.execute('''CREATE TABLE highScores
(name text, score real)''')
print("table build in success")
#check if table alreay exist in this file
#if not exist, it will build one.
except:
print("table exist")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# create the socket
print ('socket created')
try:
s.bind((HOST, PORT))
except socket.error as err:
print("bind failed")
#close if bund failed
sys.exit()
print ('Socket Bind Success!')
# start listen
s.listen(1)
print ('Socket is now listening')
while 1:
conn, addr = s.accept()
print ('Connect with ' + addr[0] + ':' + str(addr[1]))
#connect with client
buf = conn.recv(64)
print (buf)
playerName, playerScore ='', ''
for i in buf:
if i in '0123456789':
playerScore += i
else:
playerName += i
finalScore = int(playerScore)
c.execute("INSERT INTO highScores (name, score) VALUES ('{}', {})".format(playerName, finalScore))
# add player data to table, and save the table
conn.commit()
#recive from client message and print it
for row in c.execute('SELECT * FROM highScores ORDER BY score'):
print(row)
# print the table by order: first place will print first, last place will print last
s.close()
#close the socket
conn.commit()
#save the table again (just for sure) and close it
conn.close()
android方面:
活动:
package com.example.appproj;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class lastScreen extends AppCompatActivity implements View.OnClickListener {
TextView tvWon;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_screen);
tvWon = (TextView) findViewById(R.id.tvWon);
btn = (Button) findViewById(R.id.lastbtn);
btn.setOnClickListener(this);
etScore = (EditText) findViewById(R.id.etScore);
etName = (EditText) findViewById(R.id.etScore);
}
@Override
public void onClick(View view) {
if (help.won) {
tvWon.setText("you won");
new MessageSender().execute(etName.getText().toString() +etScore.getText().toString());
}
else {
tvWon.setText("you lose");
}
}
}
MessageSender:
import android.os.AsyncTask;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class MessageSender extends AsyncTask<String, Void, Void> {
private Exception exception;
@Override
protected Void doInBackground(String... params) {
try {
try {
Socket socket = new Socket(ip, 5000);
PrintWriter outToServer = new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()));
outToServer.print(params[0]);
outToServer.flush();
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (Exception e) {
this.exception = e;
return null;
}
return null;
}
}