TCP客户端套接字无法在onActivityResult()内部工作

时间:2014-01-07 11:48:16

标签: android sockets tcp tcpclient voice-recognition

我正在使用android TCP客户端Socket程序,该程序在设备中运行时无响应。我在这个程序中找不到任何错误,请帮我解决这个问题。

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    protected static final int RESULT_SPEECH = 1;

    private ImageButton btnSpeak;
    private TextView txtText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtText = (TextView) findViewById(R.id.txtText);

        btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

                try {
                    startActivityForResult(intent, RESULT_SPEECH);
                    txtText.setText("");
                } catch (ActivityNotFoundException a) {
                    Toast t = Toast.makeText(getApplicationContext(),
                            "OOps! Your device doesn't support Speech to Text",
                            Toast.LENGTH_SHORT);
                    t.show();
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case RESULT_SPEECH: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);


                try {

                    txtText.setText(text.get(0));
                     Socket client = new Socket("192.168.1.104", 4020);  //connect to server
                     PrintWriter printwriter = new PrintWriter(client.getOutputStream(),true);
                    printwriter.write(text.get(0));  //write the message to output stream

                    printwriter.flush();
                    printwriter.close();
                    client.close();   //closing t\e connection

                   } catch (UnknownHostException e) {
                       System.out.println("ONE");
                    e.printStackTrace();
                   } catch (IOException e) {
                       System.out.println("TWO");
                    e.printStackTrace();
                   }
            }
            break;
        }

        }
    }
}

logcat的

01-07 17:45:53.367: W/System.err(27224): java.net.SocketException: Permission denied
01-07 17:45:53.414: W/System.err(27224):    at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
01-07 17:45:53.414: W/System.err(27224):    at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
01-07 17:45:53.414: W/System.err(27224):    at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
01-07 17:45:53.414: W/System.err(27224):    at java.net.Socket.startupSocket(Socket.java:717)
01-07 17:45:53.414: W/System.err(27224):    at java.net.Socket.tryAllAddresses(Socket.java:150)
01-07 17:45:53.414: W/System.err(27224):    at java.net.Socket.<init>(Socket.java:209)
01-07 17:45:53.414: W/System.err(27224):    at java.net.Socket.<init>(Socket.java:176)
01-07 17:45:53.414: W/System.err(27224):    at net.viralpatel.android.speechtotextdemo.MainActivity.onActivityResult(MainActivity.java:78)
01-07 17:45:53.414: W/System.err(27224):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
01-07 17:45:53.414: W/System.err(27224):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
01-07 17:45:53.414: W/System.err(27224):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
01-07 17:45:53.414: W/System.err(27224):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
01-07 17:45:53.414: W/System.err(27224):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
01-07 17:45:53.414: W/System.err(27224):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 17:45:53.414: W/System.err(27224):    at android.os.Looper.loop(Looper.java:130)
01-07 17:45:53.414: W/System.err(27224):    at android.app.ActivityThread.main(ActivityThread.java:3687)
01-07 17:45:53.421: W/System.err(27224):    at java.lang.reflect.Method.invokeNative(Native Method)
01-07 17:45:53.421: W/System.err(27224):    at java.lang.reflect.Method.invoke(Method.java:507)
01-07 17:45:53.421: W/System.err(27224):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-07 17:45:53.421: W/System.err(27224):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-07 17:45:53.421: W/System.err(27224):    at dalvik.system.NativeStart.main(Native Method)
01-07 17:45:53.437: W/IME_TAG(1510): unbindCurrentClientLocked:: mCurClient.client:com.android.internal.view.IInputMethodClient$Stub$Proxy@409c6758,mCaller.obtainMessageIO(MSG_UNBIND_METHOD, mCurSeq, mCurClient.client):{ what=3000 when=-13h21m11s837ms arg1=719 obj=com.android.internal.view.IInputMethodClient$Stub$Proxy@409c6758 }
01-07 17:45:53.437: W/IME_TAG(1510): unbindCurrentClientLocked:: try 

2 个答案:

答案 0 :(得分:1)

在清单中包含网络权限,并将System.out.println更改为Log.d()以输出到Logcat。

答案 1 :(得分:0)

最好将网络代码放在一个单独的线程中,然后启动一个你想要的线程