我正在写一个udp客户端,但它似乎每次都抛出一个错误,我环顾四周,关于这个错误的所有其他问题似乎都没有在清单中添加互联网权限但我已经完成了这个,我仍然收到这个错误。
AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hardy.com.jamprototype" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SearchActivity"
android:label="@string/title_activity_search" >
</activity>
</application>
<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" />
</manifest>
myActivity.java
package hardy.com.jamprototype;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class SearchActivity extends AppCompatActivity {
ConnectionData data;
EditText etIdAdress;
byte[] send_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
data = new ConnectionData();
etIdAdress = (EditText)findViewById(R.id.idAddress);
Button btnSend = (Button)findViewById(R.id.btnSend);
Toast.makeText(getBaseContext(),data.getIpAddress() + " - " + data.getPORT(),Toast.LENGTH_LONG).show();
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mesg = etIdAdress.getText().toString();
try {
client(mesg);
} catch (IOException e) {
e.printStackTrace();
Log.d("sending: ",e.getMessage());
Toast.makeText(SearchActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void client(String mesg) throws IOException
{
DatagramSocket socket = new DatagramSocket(data.getPORT());
InetAddress ipAddress = InetAddress.getByName(data.getIpAddress());
send_data = mesg.getBytes();
DatagramPacket send_packet = new DatagramPacket(send_data,mesg.length(),ipAddress,data.getPORT());
socket.send(send_packet);
}
}
任何信息都会很棒,谢谢你们。
答案 0 :(得分:0)
你的许可是上限?为什么?变化:
<uses-permission android:name="ANDROID.PERMISSION.INTERNET" />
到
<uses-permission android:name="android.permission.INTERNET" />
为了验证,我将我的工作应用程序的权限更改为您拥有的内容,并且由于Internet权限确实崩溃了。此外,根据android studio的上下文帮助,您的<uses-permission>
代码应该放在<application>
代码之前。