2015-12-10 2 views
-1

Мой сервер не будет подключаться/запускаться вообще.Android-сервер не запускается

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. Ummm, некоторые подробности - вчера я провел несколько часов, прежде чем выяснять, что мне нужен сокет в своем потоке для Android, чтобы запустить его, задаваясь вопросом, почему он врезался в строку объявления сокета.

+0

Какая ошибка? – petey

+0

Не видите ошибку. Что я вижу при запуске, это изменение кнопки для завершения, но щелчок по ней возвращается на главный экран. Проверка сервера, который имеет строки Socket HOBOSocket = HOBOServer.accept(); System.out.println ("in"); Он никогда не печатает, поэтому ничего не происходит. –

+0

И IOException и UnknownHostException, похоже, не выбрасываются (я устанавливаю строку в e.toString() в случае обоих и устанавливаю текстовое поле для их удаления и ничего –

ответ

0

У меня был неправильный IP-адрес. Googling мой IP дал мне адрес, который мне нужен, в настройках сети.

+2

Возможно, вам захочется закрыть вопрос, 'быть полезным для кого-либо еще. – 323go