2017-08-26 63 views
-2

我試圖從我的手機發送文本到我的電腦通過WiFi,但我的應用程序不斷崩潰,我不知道爲什麼。這裏是一個崩潰的代碼:通過WiFi使用套接字發送文本導致應用程序崩潰

try { 
Socket socket = new Socket(RecieverIP,1755); 
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream()); 
DOS.writeUTF(String.valueOf(progressvalue)); 
socket.close(); 
} catch (IOException e) 
{ 
    e.printStackTrace(); 
}  

,並在我的電腦的結束:

import java.io.BufferedInputStream; 
    import java.io.DataInputStream; 
    import java.io.IOException; 
    import java.net.InetAddress; 
    import java.net.ServerSocket; 
    import java.net.Socket; 
    import java.net.UnknownHostException; 
    import java.util.logging.Level; 
    import java.util.logging.Logger; 

    /** 
    * 
    * @author fares Part of a project to control the led brightness on a pi, the job of this program is to receive a text over wifi and then launch a python program and pass the brightness as a parameter 
    */ 
    public class WebSocketReciever { 

     /** 
     * @param args the command line arguments 
     */ 
     public static void main(String[] args) throws IOException { 

      System.out.println("Program started"); 
      String msg_received; 
      System.out.println("Creating socket"); 
      ServerSocket socket = new ServerSocket(1755); 
      System.out.println("Socket created"); 
      Socket clientSocket = socket.accept();  //This is blocking. It will wait. 
      System.out.println("Client socket created"); 

      while(true){ 
      System.out.println("Reading data"); 
      DataInputStream DIS = new DataInputStream(clientSocket.getInputStream()); 
      msg_received = DIS.readUTF(); 
      System.out.println(msg_received); 

      clientSocket.close(); 
      socket.close();} 

     } 
    } 

如果我運行的PC代碼總是隻達到輸出:「套接字創建」

全代碼爲:

package com.example.fares.ledslider; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.SeekBar; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.net.Socket; 

import static com.example.fares.ledslider.R.id.seekBar; 

public class MainActivity extends AppCompatActivity { 

    private static TextView textView; 
    private static SeekBar seek_Bar; 
    private static String RecieverIP; 
    private static EditText IPText; 

    public void seekbarmethod(){ 
     seek_Bar = (SeekBar) findViewById(seekBar); 
     textView = (TextView) findViewById(R.id.textview); 
     seek_Bar.setMax(100); //Max value of the seekbar 
     seek_Bar.setProgress(50);//Initial seekbar value 
     textView.setText("Brightness = " +seek_Bar.getProgress() + " %"); //Notify user of percentage brightness 
     seek_Bar.setOnSeekBarChangeListener(
       new SeekBar.OnSeekBarChangeListener(){ 
        int count =0; 
        int progressvalue; 
        @Override 
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) { 
         progressvalue = i; 
         textView.setText("Brightness = " + progressvalue + " %"); 

          // Toast.makeText(MainActivity.this,"Change in progress" + count,Toast.LENGTH_SHORT).show(); 
          //count +=1; 
         //Send data from app to pc 

         try { 
          Socket socket = new Socket(RecieverIP,1755); 
          DataOutputStream DOS = new DataOutputStream(socket.getOutputStream()); 

          DOS.writeUTF(String.valueOf(progressvalue)); 
          socket.close(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 


        } 



        @Override 
        public void onStartTrackingTouch(SeekBar seekBar) { 
         Toast.makeText(MainActivity.this,"Change initiated",Toast.LENGTH_SHORT).show(); 
        } 

        @Override 
        public void onStopTrackingTouch(SeekBar seekBar) { 
         Toast.makeText(MainActivity.this,"Change ended",Toast.LENGTH_SHORT).show(); 
         count = 0 ; 

        } 
       } 
     ); 

    } 

    public void IPBtnMethod(View v){ 
    IPText = (EditText) findViewById(R.id.IPBox); 
    RecieverIP = IPText.getText().toString(); 
    Toast.makeText(MainActivity.this,"IP = " + RecieverIP,Toast.LENGTH_SHORT).show(); 
    } 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     seekbarmethod(); 

    } 
} 

爲什麼我的應用程序崩潰,我該如何解決它?在此先感謝

+0

在Android中,您並未在主線程中進行聯網。看到這個:https://stackoverflow.com/questions/6343166/how-doi-i-fix-android-os-networkonmainthreadexception –

+0

@ ErvinSzilagyi好吧,謝謝,你知道這是爲什麼嗎? –

+1

這是Android團隊的設計決定。可能不會通過鎖定主線程來凍結您的應用程序。 –

回答

相關問題