我刚刚开始学习Android,我正在尝试从UDP客户端
成功发送一个字符串我使用PacketSender作为服务器,但我还没有到目前为止,因为我的许可被拒绝了。
以下是代码:
public class UDPStringSender extends AppCompatActivity {
Intent intent;
String ipAddress, port;
int portNumber;
boolean shouldLog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_udp_string_sender);
intent = getIntent();
ipAddress = intent.getStringExtra("ip");
port = intent.getStringExtra("port");
portNumber = Integer.parseInt(port);
Log.d("IP number is", ipAddress);
Log.d("Port number is", port);
shouldLog = true;
}
public void sendString(View view) {
EditText stringField = (EditText)findViewById(R.id.sendableString);
String sendableString = stringField.getText().toString();
DatagramSocket _datagramSocket = null;
try {
_datagramSocket = new DatagramSocket();
InetAddress serverIP = InetAddress.getByName(ipAddress);
if(shouldLog)
Log.d("InetAddress is", serverIP.toString());
DatagramPacket _datagramPacket = new DatagramPacket(sendableString.getBytes(), sendableString.length(), serverIP, portNumber);
if(shouldLog)
Log.d("Packet to be send", _datagramPacket.toString());
_datagramSocket.send(_datagramPacket);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(_datagramSocket != null) {
_datagramSocket.close();
if(shouldLog)
Log.d("is datagramSockedClosed", String.valueOf(_datagramSocket.isClosed()));
}
}
}
}
我的机器人清单:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<activity android:name=".Interface">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".UDPStringSender">
</activity>
</application>
和我的logcat一样:
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at libcore.io.IoBridge.socket(IoBridge.java:619)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:93)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at java.net.DatagramSocket.createSocket(DatagramSocket.java:157)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at java.net.DatagramSocket.<init>(DatagramSocket.java:80)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at java.net.DatagramSocket.<init>(DatagramSocket.java:65)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at co.barakastudio.layouttest.UDPStringSender.sendString(UDPStringSender.java:48)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at android.view.View.performClick(View.java:5697)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at android.widget.TextView.performClick(TextView.java:10826)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at android.view.View$PerformClick.run(View.java:22526)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at android.os.Looper.loop(Looper.java:158)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7225)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at libcore.io.Posix.socket(Native Method)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: at libcore.io.IoBridge.socket(IoBridge.java:604)
11-23 15:58:50.652 31936-31936/co.barakastudio.layouttest W/System.err: ... 17 more
我非常感谢你的时间,因为它会节省我的吨:) 欢呼声,
答案 0 :(得分:1)
引用myself:
AwarenessFence.and()
元素应位于<uses-permission>
内
元素,但在<manifest>
元素之外。换句话说,这个
很好:
<application>
但这不是:
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.commonsware.android.something.something"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="INTERNET"/>
<uses-permission android:name="ACCESS_NETWORK_STATE"/>
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<!-- cool stuff goes here -->
</application>
</manifest>
如果你手动分析你的,Android Studio会报告这个 清单(从主菜单中分析&gt;检查代码...),它不会 只需在中输入元素即可自动显示警告 错误的地方。
答案 1 :(得分:0)
据我所知,你不能在主线程中使用套接字...... Android就是不允许的。
我从未尝试过DatagramSocket,只是通常的Socket,但我认为它没有什么区别。
您应该创建一个新线程并在那里发送字符串。
快速示例(不是最佳做法,但很短;):
Thread thread = new Thread(new Runnable() {
public void run() {
// send the String here
}
});
thread.start();
这将在后台发送字符串,一旦方法run()
终止,线程就会被销毁。