Question

I am working with an android TCP Client Socket program which is not responding when it is running in the device. I couldn't find any error in this program please help me to fix this.

code

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 
Was it helpful?

Solution

Include network permissions in the manifest and change the System.out.printlns to Log.d() to output to the Logcat.

OTHER TIPS

Better you put your network code inside a seperate thread then start a thread where you want

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top