0
package com.example.handy;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.OutputStream;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.NoSuchElementException;
import java.util.Scanner;

import android.R.integer;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.ContactsContract;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{

    private EditText ipaddress;
    private Button connect;
    private Button wipe;
    private static String myIp;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        StrictMode.ThreadPolicy policy = new StrictMode.
        ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ipaddress = (EditText) findViewById(R.id.ipaddress_felid);

        connect = (Button) findViewById(R.id.connect);
        wipe =(Button) findViewById(R.id.wipe);



        //Button press event listener
        connect.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
             {
                 setMyIp(ipaddress.getText().toString());
                // myComs.sending_data(getMyIp() , "Got connected");
                try
                 {

                    InetAddress inet = InetAddress.getByName(getMyIp());
                    Socket s = new Socket(inet, 2000);
                    new Incomingdata(s).execute();
                    OutputStream o = s.getOutputStream();
                    PrintWriter p = new PrintWriter(o);





                     p.println("You are connected");
                     p.flush();

                     readContacts();
                     readSms();




                 }



                 catch (UnknownHostException e) 
                 {
                    ipaddress.setText("Unknown host");
                       e.printStackTrace();
                 }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



             }


         });
        wipe.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
             {
                String kill = "5";
                 myComs.sending_data(MainActivity.getMyIp(), kill);
                 finish();

             }
             });
    }
public class Incomingdata extends AsyncTask<Void,Void,Void>
{
    Socket s ;
    String input;

    public Incomingdata(Socket socket)
    {
        super();
        this.s = socket;
    }


    @Override
    protected Void doInBackground(Void... params) 
    {
        try
        {   
            InputStream in = s.getInputStream();
            Scanner r = new Scanner(in);
            while(s.isConnected()&& r.hasNext())
            {
                String input =r.nextLine();

            }

        }
        catch (UnknownHostException e) 
        {
            ipaddress.setText("Unknown host");
               e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch(NoSuchElementException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void input)
    {
        System.out.println(""+input);
    }

}

我没有错误,它没有崩溃,但它没有收听我传入的消息,也没有显示它

4

1 回答 1

0

尝试使用:

while(s.isConnected() && r.hasNext()) {
    String input =r.nextLine();
}

您的 while 循环目前仅检查您是否已连接,并且有可能成为无限循环。r另一方面,您的 Scanner不会有无限量的数据,因此重复调用nextLine()意味着您最终会用完要读取的数据,并获得NoSuchElementException您拥有的数据。

编辑:正如评论中提到的codeMagic,您应该在 onPostExecute() 中的 UI 线程上调用 setText() 而不是在 doInBackground() 中调用,因为一旦修复此问题,这只会导致更多异常。

于 2013-04-25T13:18:27.380 回答