Zishan Ahmed
Zishan Ahmed

Reputation: 11

Android Socket Programming Socket never connects

I am trying to create very basic network application, which will connect to google through socket, and send "GET" and print the output in a text view. but it appears that socket never connects... what am I doing wrong ??

here is the code

MainActiviy:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.*;
import java.net.*;

public class MainActivity extends Activity {

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

        TextView tva = (TextView) this.findViewById(R.id.tv);
        String str = "";


        try {
            Socket sock = new Socket("www.google.com", 80);

            str = sock.getRemoteSocketAddress().toString();

            tva.setText("Connected to: " + str);

            DataOutputStream out = new DataOutputStream(sock.getOutputStream());
            out.writeUTF("GET //");

            BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            str = br.readLine();

            while(str != null) {
                tva.setText(str);
                str = br.readLine();
            }

            out.close();
            br.close();
            sock.close();

        } catch(Exception ex) {

        }
    }
}

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.onik.netw"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.onik.netw.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Upvotes: 1

Views: 123

Answers (1)

ashoke
ashoke

Reputation: 6461

you are trying to use sockets on main UI thread, you will get exception android.os.NetworkOnMainThreadException

Move the try {} block into a separate thread. Since this will run on non-UI thread you cant access UI elements, so you need to pass the data back to main thread may be via Handler, so it can display the data received.

For a quick test, enclose that entire try block inside an anonymous thread

new Thread() {
   public void run() {
    // your try block goes here    
  }
}.start();

replace tva.setText with Log.d("test", str)

Upvotes: 1

Related Questions