无法连接到AndroidStudio中的Jedis(Java)。

huangapple 未分类评论46阅读模式
英文:

Unable to connect to Jedis in AndroidStudio (Java)

问题

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);
            }
        });
    }
}
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)
<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>
英文:

My apllication is crushing when I press the button which is responsible for connecting to Redis database using Jedis client and changing TextView to value which stores jedis.ping() (It should return "Pong").
Code and Log are below. Same operations (connection to Redis and printing jedis.ping()) works perfectly in Eclipse. Maybe I insert code connected with Jedis in wrong place? I know too litle about Android programming. Thanks in advance!

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(&quot;localhost&quot;);
                String value = jedis.ping();
                show.setText(value);
            }
        });
    }
}

Log:

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)&#160;
        at redis.clients.jedis.Connection.sendCommand(Connection.java:125)&#160;
        at redis.clients.jedis.Connection.sendCommand(Connection.java:120)&#160;
        at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:113)&#160;
        at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:188)&#160;
        at com.samfoundation.samfoundation.MainActivity$1.onClick(MainActivity.java:33)&#160;
        at android.view.View.performClick(View.java:7259)&#160;
        at android.view.View.performClickInternal(View.java:7236)&#160;
        at android.view.View.access$3600(View.java:801)&#160;
        at android.view.View$PerformClick.run(View.java:27892)&#160;
        at android.os.Handler.handleCallback(Handler.java:883)&#160;
        at android.os.Handler.dispatchMessage(Handler.java:100)&#160;
        at android.os.Looper.loop(Looper.java:214)&#160;
        at android.app.ActivityThread.main(ActivityThread.java:7356)&#160;
        at java.lang.reflect.Method.invoke(Native Method)&#160;
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)&#160;
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

AndroidManifest.xml:

&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.samfoundation.samfoundation&quot;&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

huangapple
  • 本文由 发表于 2020年4月10日 01:05:19
  • 转载请务必保留本文链接:https://java.coder-hub.com/61126444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定