Android應用程序是運行在API 19上的模擬Nexus設備上的服務器,而客戶端是將應用程序發送到服務器以顯示在其上的Java應用程序。我無法設法連接這兩個。我正在使用localhost:5001連接到android設備,連接正在被拒絕。仿真器上的Android應用程序一直在運行,並在同一端口上接受連接。我認爲端口是問題,但有誰知道我該如何正確配置?通過套接字連接Android設備和Java應用程序
Java代碼:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Server {
public static final int PORT = 5001;
public static final String IP_ADDRESS = "localhost";// "10.0.2.2";
public static Socket ss;
public static final String FILE = "slika.jpg";
public static void main(String[] args) throws InterruptedException {
boolean finished = false;
while (!finished) {
try {
System.out.println("Waiting...");
ss = new Socket(IP_ADDRESS, PORT);
System.out.println("Connected!");
OutputStream out = ss.getOutputStream();
File file = new File(FILE);
System.out.println("File size: " + file.length());
finished = true;
byte[] b = new byte[(int) file.length()];
out.write((int)file.length());
try {
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(b);
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
} catch (IOException e1) {
System.out.println("Error Reading The File.");
e1.printStackTrace();
}
out.write(b);
out.flush();
System.out.println("Finished sending!");
} catch (Exception e) {
Thread.sleep(1000);
} finally {
if (ss != null)
try {
ss.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
的Android代碼:
package com.example.filereceiver;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final int PORT = 5001;
TextView informator;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
informator = (TextView) findViewById(R.id.informator);
image = (ImageView) findViewById(R.id.image);
informator.setText("Waiting!");
Log.i("DEBUGGER","Waiting!");
new Thread() {
@Override
public void run() {
ServerSocket listener = null;
Log.i("DEBUGGER","Waiting for connection");
try {
listener = new ServerSocket(PORT);
Socket sock = listener.accept();
Log.i("DEBUGGER","Connected");
InputStream in = sock.getInputStream();
int size = in.read();
byte[] b = new byte[size];
in.read(b);
Bitmap myBitmap = BitmapFactory.decodeByteArray(b, 0, size);
image.setImageBitmap(myBitmap);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
MainActivity.this.informator
.setText("TRANSFER COMPLETE!");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Info")
.setContentText("Transfer complete!")
.setAutoCancel(true);
Intent intent = new Intent(MainActivity.this,
MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(
MainActivity.this, 0, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
});
} catch (Exception e) {
e.printStackTrace();
}finally{
if(listener!=null)try {
listener.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我在無線網絡上進行了測試,結果發生了很小的變化。但是如果有人知道什麼是實際問題,請寫下它。 – Dejan