Simple code not working... why??

  • Replies:5
  • OpenNot stickiedUnanswered
  • Forum posts: 26

Sep 24, 2016 6:24:59 PM via Website

Dear friends,

i just started to develop android apps for my smartphone. like a few days ago. I have c and c++ experiences in programming, but I am kinda new to java/android. So I would appreciate some support a lot.

One of my first apps is not working and I can't understand why. Here is the main code (BTW, I know that i definately imported too many libraries. I didn't exactly know which I really need so I imported the same stuff like the apps, from where I got code fragments, do. But I think that should not be a problem, as long as the needed libraries are there, right?

package com.example.blabla.filetclient;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Socket socket;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  //  android.os.Debug.waitForDebugger();


    try {
        this.socket = new Socket("192.168.178.32", 80);
    } catch (IOException e) {
        e.printStackTrace();
    }

    File file = new File(Environment.getExternalStorageDirectory(),
            "picture.jpg");

    byte[] bytes = new byte[(int) file.length()];
    try {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

        bis.read(bytes, 0, bytes.length);

        bos.write(bytes, 0, bytes.length);
        bos.flush();
        socket.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}


The app is supposed to connect to a server and straightly send the file "picture.jpg" which is in the external storage (I am pretty sure that the path is right and that the file really esists there (checked with es file explorer)) and then disconnect.
I did not change the layout, I mean the GUI. I left the original xml file which is from the "Hello world" app untouched, so I expected that "My Application - Hello world!" would come up, doing the job in the background.

But this is what happenes when I run the app on my motorola (not emulator):

First, 0 Gradle errors and warnings. On the phone this alert comes up "filetclient is not responding" and I can click only click on "OK". I can see, that flietclient is running in a white, empty screen, but when I try to bring it to the foreground then that error alert comes again, bringing me back to the desktop.
On server side, nothing comes. No connection attempts. My phone and the server are connected physically 100%, which I can check with other apps.

Can anybody tell me what I did wrong? Why is the code not working and the simple GUI not showing up?

Thank you many times.

  • Forum posts: 258

Sep 24, 2016 7:39:24 PM via Website

Hello, please post here app's manifest file and activity_main.xml. When the phone is connected to pc, there must be shown the logcat errors.

  • Forum posts: 26

Sep 24, 2016 8:57:26 PM via Website

Here we go (I had to replace all URLs with XXXXX because URLs are not allowed for me to post here):

<?xml version="1.0" encoding="utf-8"?>

package="blabla.filetclient">

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



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

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


xmlns:tools="XXXXX"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="blabla.filetclient.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

  • Forum posts: 26

Sep 24, 2016 10:04:39 PM via Website

unfortunately, i cannot post the complete logcat log because i get the message "your contribution contains unapproved content" :-(

last lines:

09-24 20:51:19.921 426-426/com.example.blabla.filetclient D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
09-24 20:51:19.941 426-426/com.example.blabla.filetclient D/AndroidRuntime: Shutting down VM
09-24 20:51:19.941 426-426/com.example.blabla.filetclient W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41a0a6f0)
09-24 20:51:19.951 426-426/com.example.blabla.filetclient E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blabla.filetclient/com.example.blabla.filetclient.MainActivity}: android.os.NetworkOnMainThreadException

  • Forum posts: 258

Sep 25, 2016 11:37:01 AM via Website

Error is related to Socket initialization.

  1. If your moto have Android 6.0 or greater you need to get runtime permission for the internet connection.

  2. Read about socket init on Android here

— modified on Sep 25, 2016 12:06:53 PM