android登陆socket通讯
写了一个登陆界面,输入账号密码点登陆之后连接服务器,在这里点了登陆之后,在Logcat里面输出的只有这个Log.i("log","123");,在之后的s = new Socket("10.82.11.218",8025);运行完这段话输出Log.i("log","1234");,可是就是这个连接不上,不知道哪里出现问题了。上网找了很多还是找不出,没有办法只好来求救。。。。MainActivity代码
package com.example.androidtest;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText ed1;
EditText ed2;
ServerSocket ss = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
ed1 = (EditText)findViewById(R.id.Let2);
Editable user;
user = ed1.getText();
ed2 = (EditText)findViewById(R.id.Let3);
Editable password;
password = ed2.getText();
Button bt1 = (Button)findViewById(R.id.Lbt1);
bt1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Socket s = null;
String username = ed1.toString();
String password = ed2.toString();
try {
Log.i("log","123");
s = new Socket("10.82.11.218",8025);
//向服务端发送消息
Log.i("log","1234");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
out.println(username);
out.println(password);
//接受服务端的消息
InputStreamReader isr = new InputStreamReader(s.getInputStream());
Boolean bIsr = isr.ready();
if(bIsr){
Intent intent = new Intent();
intent = new Intent(MainActivity.this,operationActivity.class);
startActivity(intent);
}
//预留----如果账号或密码不正确该显示:
out.close();
isr.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Serversocket代码:
package com.zwm.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Serversocket implements Runnable{
@Override
public void run() {
try {
ServerSocket ss = new ServerSocket(8025);
System.out.println("等待链接");
while(true){
Socket s = ss.accept();
System.out.println("链接了一次");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String username = br.readLine();
String password = br.readLine();
if(username=="zwm"&&password=="888888"){
OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
os.write("1");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]){
Serversocket c = new Serversocket();
new Thread(c).start();
}
}
--------------------编程问答-------------------- 没人帮忙吗.....................!!!
补充:移动开发 , Android