• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Simple app not working... why??

armai123mai

Lurker
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.
 
Hey,
thank you buddy. I read somewhere that AsyncTask is not suitable for tasks which last longer than a few seconds, but my app is supposed to send a file. The file could be large and take a long time to be transferred. Could this cause trouble?

Thx
 
Well AsyncTask is designed to allow interraction with UI components.
If you don't need to do this, then you could simply create a new thread to handle the file transmission. Then it doesn't matter how long the thread executes, it's not holding anything else up.
 
I made it with AsyncTask! Thank you!

It took like 5 mins to send the file, which is propably related to the server, but everything went just fine as it should.

Now my next step would be to create a MIT AppInventor Extension of this feature. In other words, I have to transform these classes (or maybe just the tcp thread/class) to an AppInventor extension, somehow. Are you familiar with this maybe? Or anyone else reading this?

Thx
 
Never used it. Best I could find was this video, with Spanish subtitles

 
Back
Top Bottom