Android服务器不会运行

时间:2015-12-10 22:44:51

标签: android sockets

我的服务器根本无法连接/运行。

package com.example.brooklynxman.hobo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Calendar;


public class Loading extends ActionBarActivity {
    int gort1=0;
    int numDates=0;
    String d1="";
    String d2="";
    String result = "";
    Button bcont;
    boolean done = false;
    boolean validity=true;
    Intent intent;
    int numPages=0;
    Socket s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        intent = getIntent();
        numDates=intent.getIntExtra("numDates",0);
        d1=intent.getStringExtra("date1");
        if (numDates ==2) {
            d2=intent.getStringExtra("date2");
        }
        SharedPreferences sp = getSharedPreferences("userinput", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("done",false);
        editor.putString("date1",d1);
        editor.putString("date2", d2);
        editor.putInt("numDates", numDates);
        bcont= (Button) findViewById(R.id.button4);
        bcont.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (done) {
                    if (validity) {
                        Intent myIntent = new Intent(v.getContext(), TableDisplay.class);
                        myIntent.putExtra("results", result);
                        myIntent.putExtra("numPages", numPages);
                        myIntent.putExtra("page", 0);
                        Loading.this.startActivity(myIntent);
                    } else {
                        Intent myIntent = new Intent(v.getContext(), MainActivity.class);
                        Loading.this.startActivity(myIntent);
                    }
                }
            }
        });
        //connect();
        //tempover();
        try {
            connect();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

        if (sp.getBoolean("done",true)) {
            if (result.equals("")) {
                result = sp.getString("results", "");
                if (!result.equals("")) {
                    numPages = sp.getInt("numPages", 0);
                    validity = true;
                } else {
                    validity = false;
                }
                bcont.setText("Complete");
                done = true;
            }
        }
        else {
            done = true;
            validity=false;
        }
    }


    private void connect() throws IOException{
        MyClientTask myClientTask = new MyClientTask();
        myClientTask.execute();
        /*SharedPreferences sp = getSharedPreferences("userinput", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        String date1;
        String date2 = "";
        int numDate = sp.getInt("NumDate",0);
        int gort = sp.getInt("gort",0);
        if (numDate==1) {
            date1 = sp.getString("Date","");
        }
        else {
            date1 = sp.getString("Date1","");
            date2=sp.getString("Date2","");
        }*/
        /*new Thread(new Runnable(){
            public void run(){
                try {
                    String SERVER_IP="73.23.84.77";
                    InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                    s = new Socket(serverAddr, 5001);//add ip
                    //end
                    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    //PrintWriter out = new PrintWriter(s.getOutputStream(), true);
                    PrintWriter out = new PrintWriter(new BufferedWriter(
                            new OutputStreamWriter(s.getOutputStream())),
                            true);
                    //out.println(numDate);
                    out.print(numDates);
                    int m, d, y;
                    m = 0;
                    d = 0;
                    y = 0;
                    //convert to int;
                    if (d2.length() < 7) {
                        d2 = "00/00/00";
                    }
                    m = Integer.parseInt(d1.substring(0, 2));
                    d = Integer.parseInt(d1.substring(3, 5));
                    y = Integer.parseInt(d1.substring(6, 8));
                    out.print(m);
                    out.print(d);
                    out.print(y);
                    if (numDates == 2) {
                        m = Integer.parseInt(d2.substring(0, 2));
                        d = Integer.parseInt(d2.substring(3, 5));
                        y = Integer.parseInt(d2.substring(6, 8));
                        out.print(m);
                        out.print(d);
                        out.print(y);
                    }
                    out.print(0);
                    out.flush();
                    String valid = in.readLine();
                    if (!valid.equals("invalid")) {
                        result = in.readLine();
                        int l = result.length() - 221;
                        numPages = (l / 37) / 17;
                        out.close();
                        in.close();
                        s.close();
                        bcont.setText("Complete");
                        if (!result.equals("")) {
                            validity = true;
                        }
                    } else {
                        in.close();
                        out.close();
                        s.close();
                        validity = false;
                    }
                    done = true;
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        /*try {
            String SERVER_IP="73.23.84.77";
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            s = new Socket(serverAddr, 5001);//add ip
            //end
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            //PrintWriter out = new PrintWriter(s.getOutputStream(), true);
            PrintWriter out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(s.getOutputStream())),
                    true);
            //out.println(numDate);
            out.print(numDates);
            int m, d, y;
            m = 0;
            d = 0;
            y = 0;
            //convert to int;
            if (d2.length() < 7) {
                d2 = "00/00/00";
            }
            m = Integer.parseInt(d1.substring(0, 2));
            d = Integer.parseInt(d1.substring(3, 5));
            y = Integer.parseInt(d1.substring(6, 8));
            out.print(m);
            out.print(d);
            out.print(y);
            if (numDates == 2) {
                m = Integer.parseInt(d2.substring(0, 2));
                d = Integer.parseInt(d2.substring(3, 5));
                y = Integer.parseInt(d2.substring(6, 8));
                out.print(m);
                out.print(d);
                out.print(y);
            }
            out.print(0);
            out.flush();
            String valid = in.readLine();
            if (!valid.equals("invalid")) {
                result = in.readLine();
                int l = result.length() - 221;
                numPages = (l / 37) / 17;
                out.close();
                in.close();
                s.close();
                bcont.setText("Complete");
                if (!result.equals("")) {
                    validity = true;
                }
            } else {
                in.close();
                out.close();
                s.close();
                validity = false;
            }
            done = true;
        }
        catch (IOException e) {
            e.printStackTrace();
        }*/
    }


    public class MyClientTask extends AsyncTask<Void, Void, Void> {
        MyClientTask(){
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            SharedPreferences spx;
            spx = getSharedPreferences("userinput", Context.MODE_PRIVATE);
            SharedPreferences.Editor ed = spx.edit();
        /*editor.putBoolean("done",false);
        editor.putString("date1",d1);
        editor.putString("date2", d2);
        editor.putInt("numDates", numDates);*/
            Socket s = null;

            try {
                String SERVER_IP="127.00.0.1";//not the ip in the actual program
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                s = new Socket(serverAddr, 5000);//add ip
                //end
                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                //PrintWriter out = new PrintWriter(s.getOutputStream(), true);
                PrintWriter out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(s.getOutputStream())),
                        true);
                //out.println(numDate);
                out.print(numDates);
                int m, d, y;
                m = 0;
                d = 0;
                y = 0;
                //convert to int;
                if (d2.length() < 7) {
                    d2 = "00/00/00";
                }
                m = Integer.parseInt(d1.substring(0, 2));
                d = Integer.parseInt(d1.substring(3, 5));
                y = Integer.parseInt(d1.substring(6, 8));
                out.print(m);
                out.print(d);
                out.print(y);
                if (numDates == 2) {
                    m = Integer.parseInt(d2.substring(0, 2));
                    d = Integer.parseInt(d2.substring(3, 5));
                    y = Integer.parseInt(d2.substring(6, 8));
                    out.print(m);
                    out.print(d);
                    out.print(y);
                }
                out.print(0);
                out.flush();
                String valid = in.readLine();
                if (!valid.equals("invalid")) {
                    result = in.readLine();
                    int l = result.length() - 221;
                    numPages = (l / 37) / 17;
                    out.close();
                    in.close();
                    s.close();
                    if (!result.equals("")) {
                        validity = true;
                    }
                } else {
                    in.close();
                    out.close();
                    s.close();
                    validity = false;
                }
                done = true;
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            ed.putString("results",result);
            ed.putInt("numPages",numPages);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
        }

    }  
}

服务器在建立连接时打印,但没有发生任何事情,但按钮显示完成并重定向到MainActivity。嗯,更多的细节是昨天我花了几个小时才发现我需要在自己的线程中的套接字为android运行它想知道为什么它在套接字声明行崩溃。

1 个答案:

答案 0 :(得分:0)

我的IP错了。谷歌搜索我的IP给了我一个我在网络设置中需要的地址。