2014-01-23 2 views
-2

Я пытаюсь создать приложение в android, которые обмениваются сообщениями и файлами с помощью Java Desktop Server. Для этого я использую Socket Programming. Я нашел код в сети. В приложении нет ошибки, когда я пытаюсь запустить клиент Android для отображения пользовательского интерфейса. Я вошел в строку и нажмите на кнопку она показывает "К сожалению приложений перестала работать" отправить Вот мой исходный код:Android Client и Java Desktop Communication с использованием Socket

JAVA SERVER КОДИРОВАНИЕ:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class Main { 

    private static ServerSocket serverSocket; 
    private static Socket clientSocket; 
    private static InputStreamReader inputStreamReader; 
    private static BufferedReader bufferedReader; 
    private static String message; 
    public Main(){} 
    public static void main(String[] args) { 

     try { 
      serverSocket = new ServerSocket(7575); //Server socket 

     } catch (IOException e) { 
      System.out.println("Could not listen on port: 7575"); 
     } 

     System.out.println("Server started. Listening to the port 7575"); 

     while (true) { 
      try { 

       clientSocket = serverSocket.accept(); //accept the client connection 
       inputStreamReader = new InputStreamReader(clientSocket.getInputStream()); 
       bufferedReader = new BufferedReader(inputStreamReader); //get the client message 
       message = bufferedReader.readLine(); 

       System.out.println(message); 
       inputStreamReader.close(); 
       clientSocket.close(); 

      } catch (IOException ex) { 
       System.out.println("Problem in message reading"); 
      } 
     } 

    } 
} 

Android Coding

activity_simple_client. xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    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=".SimpleClientActivity" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="83dp" 
     android:ems="10" 
     android:hint="@string/clientmsg" > 

     <requestFocus /> 
    </EditText> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="46dp" 
     android:text="@string/instrut" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="34dp" 
     android:text="@string/send" /> 

    </RelativeLayout> 

SimpleClientActi vity.java

package com.andro.SimpleClient; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class SimpleClientActivity extends Activity { 

private Socket client; 
private PrintWriter printwriter; 
private EditText textField; 
private Button button; 
private String messsage; 
public SimpleClientActivity(){} 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_simple_client); 
textField = (EditText) findViewById(R.id.editText1); //reference to the text field 
button = (Button) findViewById(R.id.button1); //reference to the send button 

//Button press event listener 
button.setOnClickListener(new View.OnClickListener() { 

public void onClick(View v) { 

messsage = textField.getText().toString(); //get the text message on the text field 
textField.setText("");  //Reset the text field to blank 

try { 

client = new Socket("localhost", 7575); //connect to server 
printwriter = new PrintWriter(client.getOutputStream(),true); 
printwriter.write(messsage); //write the message to output stream 

printwriter.flush(); 
printwriter.close(); 
client.close(); //closing the connection 

} catch (UnknownHostException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
}); 

} 
} 

SimpleClient Manifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.andro.SimpleClient" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="18" /> 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.andro.SimpleClient.SimpleClientActivity" 
     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> 

    </manifest> 

Am Мне нужно изменить какие-либо настройки в Затмении ?. Кто-нибудь может помочь? Заранее спасибо

Мой LOCCAT Файл

01-25 00:51:42.035: W/System.err(920): java.net.SocketException: socket failed: EACCES (Permission denied) 
    01-25 00:51:42.035: W/System.err(920): at libcore.io.IoBridge.socket(IoBridge.java:576) 
    01-25 00:51:42.095: W/System.err(920): at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201) 
    01-25 00:51:42.095: W/System.err(920): at java.net.Socket.startupSocket(Socket.java:560) 
    01-25 00:51:42.105: W/System.err(920): at java.net.Socket.tryAllAddresses(Socket.java:128) 
    01-25 00:51:42.105: W/System.err(920): at java.net.Socket.<init>(Socket.java:178) 
    01-25 00:51:42.105: W/System.err(920): at java.net.Socket.<init>(Socket.java:150) 
    01-25 00:51:42.105: W/System.err(920): at com.android.homeapp.Sendstring.run(Sendstring.java:17) 
    01-25 00:51:42.105: W/System.err(920): at java.lang.Thread.run(Thread.java:841) 
    01-25 00:51:42.105: W/System.err(920): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied) 
    01-25 00:51:42.105: W/System.err(920): at libcore.io.Posix.socket(Native Method) 
    01-25 00:51:42.105: W/System.err(920): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181) 
    01-25 00:51:42.125: W/System.err(920): at libcore.io.IoBridge.socket(IoBridge.java:561) 

    I changed my Mainactivity.java as follows but it still display "Application has Stopped" and in my logcat "MainThread doing too much of work" 

MainActivity.java

package com.andro.asynctask; 
    import java.io.IOException; 
    import java.io.PrintWriter; 
    import java.net.Socket; 
    import java.net.UnknownHostException; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 
    import android.widget.EditText; 
    import android.widget.Button; 
    import android.view.*; 
    public class MainActivity extends Activity { 
    private String mMsg; 
    private Socket client; 
    private PrintWriter writer; 
    private Asyncclass ac; 
    private EditText txt; 
    //private Button btn; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txt=(EditText)findViewById(R.id.editText1); 
    //btn=(Button)findViewById(R.id.button1); 
    } 


    class Asyncclass extends AsyncTask<String,Void, String> 
    { 
     public Asyncclass(String msg) 
     { 
     mMsg=msg; 
     } 
    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     try 
     { 
     hardtask(); 
     } 

     catch(IOException i) 
     { 
      i.printStackTrace(); 
     } 
     return null; 
    } 
    public void hardtask() throws IOException,UnknownHostException 
    { 
      client=new Socket("localhost", 7575); 
      writer=new PrintWriter(client.getOutputStream(),true); 
      writer.write(mMsg); 
      writer.flush(); 
      writer.close(); 
    } 
     } 
    public void OnClick(View view) 
    { 
    mMsg=txt.getText().toString(); 
    ac=new Asyncclass(mMsg); 
    ac.execute(); 
    } 
    @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; 
    } 
    } 
+0

Ваш LogCat расскажет вам * точно * в чем проблема. –

+0

Кроме того, что предложил @PsHegger, пройдите через [this] (http://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device). Может быть, вы пытаетесь получить доступ к 127.0.0.1 вместо 10.0.2.2 – MjZac

ответ

1

Вы пытаетесь сделать сетевые коммуникации в основном потоке, что не допускается на Android 4.0 +. Чтобы решить проблему, просто переместите свой код на другой Thread (например, вы должны посмотреть на AsyncTask).

Вы можете сделать что-то подобное:

private class SendMessage implements Runnable { 
    private String mMsg; 

    public SendMessage(String msg) { 
     mMsg = msg; 
    } 

    public void run() { 
     try { 

      client = new Socket("localhost", 7575); //connect to server 
      printwriter = new PrintWriter(client.getOutputStream(),true); 
      printwriter.write(messsage); //write the message to output stream 

      printwriter.flush(); 
      printwriter.close(); 
      client.close(); //closing the connection 

     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

А затем изменить onClick к этому:

public void onClick(View v) { 
    messsage = textField.getText().toString(); //get the text message on the text field 
    textField.setText("");  //Reset the text field to blank 

    new Thread(new SendMessage(message)).start();  // start a new thread to make the communication in the background 
} 
+0

Я очень новичок в Android-андроидах, расскажите мне, как я могу это сделать.Я пытаюсь вставить новый класс, он отображает ошибку. – user3172659

+0

отредактировал мой ответ с некоторыми примерами. – pshegger

+0

. Я снова получил ту же ошибку, что и мой файл logcat: – user3172659

0
java.net.SocketException: socket failed: EACCES (Permission denied) 

ли вы отладки в эмуляторе?

client = new Socket("localhost", 7575); //connect to server 

Пожалуйста, используя 10.0.0.2 и использовать IP-адрес компьютера, если вы используете Android телефон и убедитесь, что вы подключены к Wi-Fi (Возможность подключения к компьютеру IP)