android错误“ java.net.SocketException:套接字失败:EACCES(权限被拒绝)”

时间:2018-10-30 11:49:59

标签: java android sockets networking

我正在尝试通过TCP从Android设备向计算机发送一条简单消息。当我尝试运行此代码时,出现此错误:

  

'java.net.SocketException:套接字失败:EACCES(权限被拒绝)'

代码

public class MainActivity extends Activity implements SensorEventListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        b = (Button)findViewById(R.id.button2);
        err = (TextView)findViewById(R.id.errors);
        mSensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
        mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
        mSensorManager.registerListener(this, mRotationSensor, SENSOR_DELAY);
    } catch (Exception e) {
        Toast.makeText(this, "Hardware compatibility issue", Toast.LENGTH_LONG).show();
    }
    b.setOnClickListener(new View.OnClickListener() {
        public  void onClick(View v){
            try{

                Socket s = new Socket("10.0.0.7", 5555);
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                String outMsg = "";
                outMsg = "This is Client";
                out.write(outMsg);
                out.flush();
                out.close();
                s.close();
                err.setText("sending");
            }

            catch(Exception ex){
                err.setText("error"+ex.toString());

            }
        }
    });
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor == mRotationSensor) {
        if (event.values.length > 4) {
            float[] truncatedRotationVector = new float[4];
            System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
            update(truncatedRotationVector);
        } else {
            update(event.values);
        }
    }
}
private void update(float[] vectors) {
    float[] rotationMatrix = new float[9];
    SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
    int worldAxisX = SensorManager.AXIS_X;
    int worldAxisZ = SensorManager.AXIS_Z;
    float[] adjustedRotationMatrix = new float[9];
    SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
    float[] orientation = new float[3];
    SensorManager.getOrientation(adjustedRotationMatrix, orientation);
    float pitch = orientation[1] * FROM_RADS_TO_DEGS;
    float roll = orientation[2] * FROM_RADS_TO_DEGS;
    ((TextView)findViewById(R.id.pitch)).setText("Pitch: "+pitch);
    ((TextView)findViewById(R.id.roll)).setText("Roll: "+(roll-90));
}

}

我该如何解决?或者,请重定向到该主题的教程。

1 个答案:

答案 0 :(得分:0)

在您的AndroidManifest.xml文件中添加INTERNET权限:

<uses-permission android:name="android.permission.INTERNET"/>

并且请勿在UI线程中访问,Android禁止这样做。您必须在其他线程中访问互联网。

按照以下代码段更改代码:

b.setOnClickListener(new View.OnClickListener() {
    public  void onClick(View v){
       new Thread(){
          @Override
          public void run() {
             try{

                Socket s = new Socket("10.0.0.7", 5555);
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                String outMsg = "";
                outMsg = "This is Client";
                out.write(outMsg);
                out.flush();
                out.close();
                s.close();
                runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       err.setText("sending");
                   }
                });
             }catch(Exception ex){
                runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       err.setText("error"+ex.toString());
                   }
                });

             }               
        }
    }.start();

}

});