wifi通过eclipse发送数据

wifi通过eclipse发送数据

本文介绍了wifi通过eclipse发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码而且我不知道如何解决它

在捕获错误时告诉我

I have code and I didn't know how can I solve it
when catching the error tell me

Java.Lang.NullPointerException 

Spatially in error3







package com.app.ServerClient;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;

public class ServerClient extends Activity {
		private Button bt;
	   private TextView tv;
	   private Socket socket;
	   private String serverIpAddress = "10.0.2.2";
	   // AND THAT'S MY DEV'T MACHINE WHERE PACKETS TO
	   // PORT 5000 GET REDIRECTED TO THE SERVER EMULATOR'S
	   // PORT 6000
	   private static final int REDIRECTED_SERVERPORT = 5000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		 bt = (Button) findViewById(R.id.myButton);
	      tv = (TextView) findViewById(R.id.myTextView);
	      try {
	         InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
	         socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
	      } catch (UnknownHostException e1) {
	         e1.printStackTrace();
	      } catch (IOException e1) {
	         e1.printStackTrace();
	      }
	      bt.setOnClickListener(new OnClickListener() {
	         public void onClick(View v) {
	            try {
	               EditText et = (EditText) findViewById(R.id.EditText01);
	               String str = et.getText().toString();
	               PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
	               out.println(str);
	               Log.d("Client", "Client sent message");
	            } catch (UnknownHostException e) {
	               tv.setText("Error1");
	               e.printStackTrace();
	            } catch (IOException e) {
	               tv.setText("Error2");
	               e.printStackTrace();
	            } catch (Exception e) {
	               tv.setText("Error3");
	               Toast.makeText(getBaseContext(), e+"", Toast.LENGTH_LONG).show();
	            }
	         }
	      });

	}

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

}

推荐答案


这篇关于wifi通过eclipse发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 15:40