无法连接到androidstudio(java)中的绝地武士

qkf9rpyu  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(282)

当我按下负责使用jedis客户端连接redis数据库的按钮,并将textview更改为存储jedis.ping()的值(它应该返回“pong”)时,我的应用程序崩溃了。代码和日志如下。同样的操作(连接到redis和printing jedis.ping())在eclipse中也能完美地工作。也许我把绝地武士的密码插错地方了?我对android编程知道得太少了。提前谢谢!

package com.samfoundation.samfoundation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import redis.clients.jedis.Jedis;

public class MainActivity extends AppCompatActivity {
    Button click;
    TextView show;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        click = (Button)findViewById(R.id.button);
        show = (TextView)findViewById(R.id.textView);

        click.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                Jedis jedis = new Jedis("localhost");
                String value = jedis.ping();
                show.setText(value);
            }
        });
    }
}

日志:

2020-04-09 19:53:22.473 32666-32666/com.samfoundation.samfoundation E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.samfoundation.samfoundation, PID: 32666
    redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host localhost:6379
        at redis.clients.jedis.Connection.connect(Connection.java:204)
        at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100)
        at redis.clients.jedis.Connection.sendCommand(Connection.java:125)
        at redis.clients.jedis.Connection.sendCommand(Connection.java:120)
        at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:113)
        at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:188)
        at com.samfoundation.samfoundation.MainActivity$1.onClick(MainActivity.java:33)
        at android.view.View.performClick(View.java:7259)
        at android.view.View.performClickInternal(View.java:7236)
        at android.view.View.access$3600(View.java:801)
        at android.view.View$PerformClick.run(View.java:27892)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
        at java.net.Socket.createImpl(Socket.java:492)
        at java.net.Socket.getImpl(Socket.java:552)
        at java.net.Socket.setReuseAddress(Socket.java:1493)
        at redis.clients.jedis.Connection.connect(Connection.java:171)
        at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100) 
        at redis.clients.jedis.Connection.sendCommand(Connection.java:125) 
        at redis.clients.jedis.Connection.sendCommand(Connection.java:120) 
        at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:113) 
        at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:188) 
        at com.samfoundation.samfoundation.MainActivity$1.onClick(MainActivity.java:33) 
        at android.view.View.performClick(View.java:7259) 
        at android.view.View.performClickInternal(View.java:7236) 
        at android.view.View.access$3600(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27892) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

androidmanifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samfoundation.samfoundation">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题