[英]Android app crashing when opening Client Socket
我正在開發使用客戶端/服務器體系結構的android應用程序,但出現錯誤,我有一個以Java應用程序形式構建的服務器,而客戶端是android應用程序,我的問題是當我單擊連接按鈕時,服務器顯示“ Client Connected”(客戶端已連接),這意味着客戶端套接字運行良好,但是此后我的應用程序崩潰了。
客戶端代碼(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>
更改您的客戶端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();
}
}
}
您不能在AsyncTask的doInBackground方法中使用任何視圖/ UI內容。
在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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.