我正在尝试使用套接字将数据库文件从python服务器(PC)传输到java应用程序客户端(Android-studio)我能够毫无问题地传输文件,但是当我传输数据库文件时我无法使用它android studio出现此错误:
(11) malformed database schema (?)
E/DefaultDatabaseErrorHandler: Corruption reported by sqlite on database: /data/user/0/com.example.serverclient/databases/trempi.db
deleting the database file: /data/user/0/com.example.serverclient/databases/trempi.db
**`Server(Python)`**
here is my python server:
import socket
TCP_IP = '192.168.14.87'
TCP_PORT = 9012
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(5)
print ('listening')
conn, addr=s.accept()
print ("Client connected: ",addr)
filename="trempi.db"
file=open(filename, 'rb')
file_data=file.read(4096)
conn.send(file_data)
print ("Data has been transmitted :)")
**Client(Android-Studio)**
and here is my MainActivity in android studio:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=findViewById(R.id.tv1);
btn=findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Thread myThread=new Thread(new MyServer());
myThread.start();
}
class MyServer implements Runnable
{
Handler handler=new Handler();
@Override
public void run() {
try {
byte[] b=new byte[100000];
Socket s=new Socket("192.168.14.87",9012);
InputStream is=s.getInputStream();
FileOutputStream fr=new FileOutputStream(getDatabasePath("trempi.db").toString());
is.read(b,0,b.length);
fr.write(b,0,b.length);
s.close();
is.close();
fr.close();
Intent go=new Intent(MainActivity.this,Data.class);
startActivity(go);
} catch (IOException e) {
e.printStackTrace();
}
}
}
比较原始数据库文件和接收的文件的长度。除非原始文件的长度不超过4096字节,否则file.read(4096)
仅读取数据库的第一部分,并且您会收到截断的损坏文件。尝试使用file.read()
。