I am writing a udp client but it seems to be throwing a error out every time, I've looked around and all the other questions about this error seems to be not adding the internet permission in the manifest but I've done this and I'm still getting this error.
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); } } Any Information would be great thanks guys.
No comments:
Post a Comment