繁体   English   中英

Android 应用程序在 Android Studio 中运行良好,但在设备上无法运行

[英]Android App works fine in Android Studio but doesnt work on device

所以我混杂了 AndroidStudio 和 python 来创建一个简单的应用程序,它从 python 脚本接收一个数组字符串并将其显示在 ListView 中。 在 AndroidStudio 中设置时一切正常,应用程序启动,从 python 脚本获取数组并显示它。 但是,当将它导出到 APK 并在我的 S22 上安装和运行它时,它不显示数组。 python 脚本也没有注册任何东西。 应用程序:

package com.example.app;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    // Array of strings to display in the ListView
    List<String> array = new ArrayList<>();

    // The ListView
    ListView listView;

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

        // Find the ListView in the layout
        listView = findViewById(R.id.list_view);

        // Create an adapter to convert the array to views
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array);

        // Attach the adapter to the ListView
        listView.setAdapter(adapter);

        // Start the AsyncTask to connect to the server and receive the array of strings
        new ReceiveArrayTask().execute();
    }

    private class ReceiveArrayTask extends AsyncTask<Void, Void, List<String>> {

        @Override
        protected List<String> doInBackground(Void... params) {
            List<String> array = new ArrayList<>();

            // Connect to the server and receive the array of strings
            try {
                // Convert the server's hostname to an IP address
                InetAddress serverAddress = InetAddress.getByName("IP FROM PC");

                Socket socket = new Socket(serverAddress, 6666);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                // Read the array of strings from the server
                String line;
                while ((line = in.readLine()) != null) {
                    array.add(line);
                }
                in.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return array;
        }

        @Override
        protected void onPostExecute(List<String> array) {
            // Update the ListView with the received array of strings
            ArrayAdapter<String> adapter = (ArrayAdapter<String>) listView.getAdapter();
            adapter.clear();
            adapter.addAll(array);
            adapter.notifyDataSetChanged();
        }
    }
}

这是 python 脚本:

import socket

# Set the hostname or IP address and port number of the server
HOSTNAME_OR_IP_ADDRESS = "IP FROM PC"
PORT_NUMBER = 6666

# Create a socket and bind it to the hostname or IP address and port number
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOSTNAME_OR_IP_ADDRESS, PORT_NUMBER))

# Listen for incoming connections
server_socket.listen()

print("Listening for incoming connections on " + HOSTNAME_OR_IP_ADDRESS + ":" + str(PORT_NUMBER))

# Accept an incoming connection
connection, client_address = server_socket.accept()

print("Received connection from " + str(client_address))

# Send the array of strings to the client
array = ["item1", "item2", "item5", "item76"]
for item in array:
    connection.send((item + "\n").encode())

# Close the connection
connection.close()

确认您可以从您的设备访问您的 PC

  1. 使用adb (USB 或 wifi)连接设备
  2. 打开一个 shell adb shell
  3. 从那个 shell ping 你的 PC 地址
  4. 如果上一步成功使用netcat到您的 PC 和端口
  5. 如果成功,您的应用程序应该可以运行(如果它具有权限)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM