如何在genymotion上运行客户端服务器?

时间:2016-01-31 18:14:23

标签: java android genymotion

我是Android编程的初学者。我遇到了eclips和genymotion的问题。这是我的代码:

package app.server;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class ServersideActivity extends Activity {

    /** Called when the activity is first created. */

    private EditText edttxt;
    private Button   btn;
    private TextView txtview;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        edttxt = (EditText) findViewById(R.id.edttxt);
        btn = (Button) findViewById(R.id.btn);
        txtview = (TextView) findViewById(R.id.txtview);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String a = new String();
                a = edttxt.getText().toString();
                txtview.setText("hamid");
                // TODO Auto-generated method stu
                txtview.setText(a);

            }
        });

        DatagramSocket socket = null;
        try {
            socket = new DatagramSocket(1238);
        }
        catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte[] buffer = new byte[1024];
        DatagramPacket request = new DatagramPacket(buffer, buffer.length);
        try {
            socket.receive(request);
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sec_packet avali = new sec_packet(5, 3, "avvali");
        System.out.println(avali.tosend);
        sec_packet dovom = new sec_packet("15\t65\tavvali");
        String data = new String(request.getData());
        String pm_vorodi = new String(data.trim());
        sec_packet sevom = new sec_packet(pm_vorodi);
        System.out.println("" + sevom.pay);
        if (sevom.ack == 5)
            System.out.println("OK");
        else
            System.out.println("NO");
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("out.txt");
            byte[] aa = new byte[1000];
            aa = sevom.pay.getBytes();
            out.write(aa);
            out.close();
        }
        catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        String response = "15\t65\tavvali";
        DatagramPacket reply = new DatagramPacket(response.getBytes(), response.length(), request.getAddress(), request.getPort());
        try {
            socket.setSoTimeout(20000);
        }
        catch (SocketException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            socket.send(reply);
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    public static void waitTime(long millisecond) {
        long max = millisecond;
        for (long i = 0; i < max; i++) {
            for (long j = 0; j < max; j++) {

            }
        }
    }


    static class sec_packet {

        public int    seq;
        public int    ack;
        public String pay;
        public String tosend;


        sec_packet(int seq_no, int ack_no, String payload) {
            // TODO Auto-generated constructor stub
            seq = seq_no;
            ack = ack_no;
            pay = payload;
            tosend = Integer.toString(seq) + '\t' + Integer.toString(ack) + '\t' + pay;
        }


        sec_packet(String tosend) {
            int len = tosend.length();
            int i = 0;
            int seq_found = 0;
            int ack_found = 0;
            int pose1 = 0;
            char x;
            for (i = 0; i < len; i++)
            {
                x = tosend.charAt(i);
                if (x == '\t')
                    if (seq_found == 0)
                    {
                        seq = Integer.parseInt(tosend.substring(0, i));
                        seq_found = 1;
                        pose1 = i;
                    }
                    else
                    {
                        ack = Integer.parseInt(tosend.substring(pose1 + 1, i));
                        ack_found = 1;
                        pay = tosend.substring(i + 1, len);
                        break;
                    }

            }
            if (seq_found == 0 || ack_found == 0)
            {
                System.out.println("not found");
            }
        }
    }

}

这只是服务器代码。当我将它作为简单的java程序运行时,它可以正常工作。但是当我在Genymotion上运行时,我得到的结果是:   to see click here 我不知道该怎么办?请帮帮我。

1 个答案:

答案 0 :(得分:1)

你有NetworkOnMainThreadException,因为你在onCreate()而不是AsyncTask或线程中执行网络代码。