- Forum posts: 41
Sep 24, 2016, 6:24:59 PM via Website
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.
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.