当前位置: 首页 > 知识库问答 >
问题:

android错误“java.net.socketexception:套接字失败:EACCES(权限被拒绝)”[重复]

曾高杰
2023-03-14
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个答案

钱元徽
2023-03-14

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

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

并且不要在UI线程中访问,Android禁止这样做.必须在其他线程中访问internet.

按以下代码段更改代码

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();

}
 类似资料: