2015-04-30 148 views
0

我正在開發一個使用客戶端/服務器體系結構的Android應用程序,我有一個錯誤我有一個服務器,我作爲一個Java應用程序構建,並作爲Android應用程序的客戶端,我的問題是當我點擊連接按鈕,服務器顯示客戶端連接,這意味着客戶端套接字很好,但我的應用程序崩潰後。Android應用程序崩潰時打開客戶端套接字

客戶端代碼(安卓)

package com.example.androidpc; 

import java.io.IOException; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.app.Activity; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class Home extends Activity { 

private EditText ip_txt,port_txt; 
private ImageView cnt_btn; 
Context ctx = this; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 

    ip_txt = (EditText) findViewById(R.id.ip_txt); 
    port_txt = (EditText) findViewById(R.id.port_txt); 
    cnt_btn = (ImageView) findViewById(R.id.con_img); 

    cnt_btn.setClickable(true); 

    cnt_btn.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      new Client().execute(ip_txt.getText().toString(),port_txt.getText().toString()); 

     } 

    }); 

} 

private class Client extends AsyncTask<String,Void,String>{ 

    Socket s; 
    @Override 
    protected String doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 

     try { 
      s = new Socket(arg0[0],Integer.parseInt(arg0[1])); 

      Toast.makeText(ctx, "Connection Established", Toast.LENGTH_SHORT).show(); 
      s.close(); 

     } catch (NumberFormatException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return null; 
    } 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.home, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

服務器(Java)的

import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 


public class Server { 

    public static void main(String[] args) throws IOException { 
     ServerSocket ss = new ServerSocket(3333); 
     System.out.println("Server is listening on port 3333"); 
     Socket s = ss.accept(); 
     System.out.println("Client Connected"); 
     s.close(); 
     ss.close(); 
    } 

} 

Android清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.androidpc" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="14" 
     android:targetSdkVersion="21" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".Home" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

活動XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#2A095C" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.androidpc.Home" > 

    <ImageView 
     android:id="@+id/logo_img" 
     android:contentDescription="imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:src="@drawable/applogo" /> 

    <EditText 
     android:id="@+id/ip_txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:ems="10" 
     android:hint="Server IP Address" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/port_txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/ip_txt" 
     android:layout_below="@+id/ip_txt" 
     android:ems="10" 
     android:hint="Port" /> 

    <ImageView 
     android:id="@+id/con_img" 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:layout_below="@+id/port_txt" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="0dp" 
     android:src="@drawable/notconnected" /> 

</RelativeLayout> 
+0

認沽'Toast.makeText(CTX, 「已建立連接」,Toast.LENGTH_SHORT).show();''裏面onPostExecute '是從'doInBackground'中移除。 'Toast'是UI線程的更新,不能在'doInBackground'內完成。提供來自'AsyncTask的UI更新,onPostExecute'。 –

+0

我會嘗試,讓你知道結果 – Tomahawk

+0

顯示你的logcat –

回答

0

更改客戶機的AsyncTask:

private class Client extends AsyncTask<String,Void,String>{ 

    Socket s; 
    @Override 
    protected String doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 

     try { 
      s = new Socket(arg0[0],Integer.parseInt(arg0[1])); 
      s.close(); 
      return "Connection Established" 
     } catch (NumberFormatException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return null; 
    } 
    @Override 
    protected void onPostExecute(String result){ 
     if (result != null) { 
      Toast.makeText(ctx,result , Toast.LENGTH_SHORT).show();   
     } else { 
      Toast.makeText(ctx,"Error" , Toast.LENGTH_SHORT).show();     
     } 
    } 
} 
0

不能使用在doInBackground方法的任何視圖/ UI內容的AsyncTask。

使用在onProgressUpdate方法您的UI內容

試試這個:

private class Client extends AsyncTask<Void,Void,Void>{ 

    Socket s; 
    @Override 
    protected Void doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 
     publishProgress(); 
    } 


protected void onProgressUpdate() { 

     try { 
      s = new Socket(arg0[0],Integer.parseInt(arg0[1])); 

      Toast.makeText(ctx, "Connection Established", Toast.LENGTH_SHORT).show(); 
      s.close(); 

     } catch (NumberFormatException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } 

} 
相關問題