這裡有一個有趣的。 我正在試圖開發一個 Android 應用,它允許用戶控制in中的相機。 你可以告訴Maya在特定埠上監聽( Maya語言) 命令腳本腳本。 我寫了這個應用程序來發送命令,我得到了以下錯誤:09-21 14:07:34.021 23651-23651/com.jvspad.mayaterm I/System.out﹕ Begin.... 09-21 14:07:34.071 23651-24337/com.jvspad.mayaterm E/TCP Client﹕ C: Connecting... 09-21 14:07:34.182 23651-24337/com.jvspad.mayaterm E/TCP﹕ C: Error
java.net.ConnectException: failed to connect to/10.0.0.2 (port 1024): connect failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.connect(IoBridge.java:114)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.Socket.startupSocket(Socket.java:566)
at java.net.Socket.(Socket.java:225)
at com.jvspad.mayaterm.DisplayMessageActivity$SendCommandTask.doInBackground(DisplayMessageActivity.java:71)
at com.jvspad.mayaterm.DisplayMessageActivity$SendCommandTask.doInBackground(DisplayMessageActivity.java:62)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
at libcore.io.Posix.connect(Native Method)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.Socket.startupSocket(Socket.java:566)
at java.net.Socket.(Socket.java:225)
at com.jvspad.mayaterm.DisplayMessageActivity$SendCommandTask.doInBackground(DisplayMessageActivity.java:71)
at com.jvspad.mayaterm.DisplayMessageActivity$SendCommandTask.doInBackground(DisplayMessageActivity.java:62)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
到目前為止我已經排除了IP地址和埠。 我第一次收到這個錯誤時,也在路由器上轉發了我的埠。 你們能幫我排除在我的安卓應用程序中的可能性? 下面是 Activity的相關代碼。package com.jvspad.mayaterm;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
//Using http://cs.lmu.edu/~ray/notes/javanetexamples/as bases for connection...
import java.net.UnknownHostException;
import java.net.Socket;
import java.net.InetAddress;
import java.io.*;
import android.util.Log;
public class DisplayMessageActivity extends Activity {
private static final int SERVERPORT = 1024;
private static final String SERVERIP ="10.0.0.2";
//private OnMessageReceived mMessageListener = null;
private boolean mRun = false;
PrintWriter out;
BufferedReader in;
@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("Begin....");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(TerminalWindow.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
new SendCommandTask().execute("URL");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//Handle action bar item clicks here. The action bar will
//automatically handle clicks on the Home/Up button, so long
//as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class SendCommandTask extends AsyncTask {
@Override
protected String doInBackground(String... urls) {
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.e("TCP Client","C: Connecting...");
//Make a socket to connect....
Socket socket = new Socket(serverAddr, SERVERPORT);
try {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
} catch (Exception e) {
Log.e("TCP","S: Error", e);
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
//after it is closed, which means a new socket instance has to be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP","C: Error", e);
}
return"Comeplete";
}
}
}
這是我的Manifest:<?xml version="1.0" encoding="utf-8"?>
package="com.jvspad.mayaterm">
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
android:name=".TerminalWindow"
android:label="@string/app_name">
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName=".TerminalWindow">
android:name="android.support.PARENT_ACTIVITY"
android:value="com.jvspad.mayaterm.TerminalWindow"/>
感謝你的幫助 !