1

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>

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.