我不是Android的专家,很抱歉,如果我的问题看起来很基本。昨晚我花了很多时间来查找它并找不到解决方案。
基本上,当我按下菜单中的按钮时,会调用onOptionsItemSelected方法来创建一个带服务器的套接字。但是,创建它所需的IP和端口具有初始值(null和零)。
出于这个原因,我尝试使用cd folder_location
echo ${file_prefix}*.txt | cut -d '_' -f 3 | sort -u | \
while read suffix ; do
[ -d ${suffix} ] || mkdir ${suffix}
mv ${file_prefix}*_${suffix}_*.txt ${suffix}/
done
将SharedPreferences
保存为以下代码:
remotePort
此外,我尝试使用public class PlayActivity
{
private static final String TAG = "PlayActivity";
private SocketClient thread;
private WebsocketPortClient websocketClient;
private static String remoteIP;
private static int remotePort;
...
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_main);
//request port
context = this;
websocketClient = new WebsocketPortClient(event.getID(), event.getTitle(), app.getMyCode());
initWebsocketClient();
...
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.userComment:
...
break;
case R.id.userCamera: // Camera Button
if (flPreview.getVisibility() == View.VISIBLE) {
...
} else {
// Here I Get the IP and Port
SharedPreferences sharedPref = getSharedPreferences(getString(R.string
.preference_file_key),
Context.MODE_PRIVATE);
remoteIP = sharedPref.getString(getString(R.string.ip), "192.168.1.160");
remotePort = sharedPref.getInt(getString(R.string.port), 8880);
Log.e(TAG, "Connecting to the server: " + remoteIP + " on port: " + remotePort);
if (started) {
if (remoteIP == null) {
thread = new SocketClient(preview);
} else {
thread = new SocketClient(preview, remoteIP, remotePort);
}
started = false;
flPreview.setVisibility(View.VISIBLE);
cameraOnBtn.setIcon(R.drawable.act_user_camera_ov);
} else {
closeSocketClient();
reset();
}
}
break;
case android.R.id.home:
askFinish();
break;
}
return true;
}
private void initWebsocketClient()
{
assigned = new AtomicBoolean(false);
websocketClient.setListener(new WebsocketPortClient.WebsocketPortListener()
{
@Override
public void onPortAssigned(int port)
{
if (portRequester.isPortReceived()) {
SharedPreferences sharedPref = getSharedPreferences(getString(R.string
.preference_file_key),
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.putInt(getString(R.string.port), portRequester.getAssignedPort());
editor.commit();
Log.e(TAG, "onPortAssigned: " + sharedPref.getInt(getString(R.string.port),
8880));
supportInvalidateOptionsMenu();
assigned.set(true);
}
}
});
}
...
}
AtomicInteger
,但结果相同。该值始终为零。现在使用remotePort
,我得到的值是先前存储的值,您可以在此日志中看到:
SharedPreferences
我真的想知道如何更新onOptionsItemSelected内的E/socketPort: connect(): ws://portassigner.net:8080/ControllerServer/actions
E/socketPort: jsonObj sent: {"action":"add","eventID":"542398d26b980","eventTitle":"테트","userID":"6da557b7d92"}
E/socketPort: jsonObj received: {"action":"add","id":0,"eventID":"542398d26b980","eventTitle":"테트","userID":"6da557b7d92","status":"Off","color":"color02","assignedPort":8680}
E/socketPort: Action: add
E/socketPort: Add Port: 8680
E/tya: Running: 4, Top: ComponentInfo{kseek.stime/kseek.stime.event.play.PlayActivity}
E/MediaPlayer: Should have subtitle controller already set
E/tya: EventLoadingActivity::Finish()
E/tya: GC_RECV << SC;I;542398d26b980:110:1/0/1/20000; :
E/PlayActivity: Connecting to the server: 192.168.1.160 on port: 8330
E/socket: java.net.ConnectException: failed to connect to /192.168.1.160 (port 8330) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused)
值。使用在创建活动时创建的侦听器更新端口。
先谢谢你们的帮助。
答案 0 :(得分:0)
你应该做什么?
在知道值后,在共享首选项中设置remoteIP的值。您还没有在代码中的任何位置设置它的值。但是你读了它
为什么?
您尚未设置
的值 代码中的任何地方都有remoteIP。
这就是为什么
remoteIP = sharedPref.getString(getString(R.string.ip),&#34; 192.168.1.160&#34;);
没有你的IP。
您获得默认端口值,因为
if(remoteIP == null)始终为true。