我将此方法称为Java桌面应用程序,它可以正常工作,但是当我在android应用程序中调用它时,该方法无法正常工作。请帮我什么问题。我被添加了权限 在清单文件中。
这是错误:
W / System.err:android.os.NetworkOnMainThreadException
private static String sendmessage(String host, String sendMessage){
Socket socket=new Socket();
String getmessage="";
try
{
int port = 80;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(sendMessage);
bw.flush();
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
getmessage = br.readLine();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return getmessage;
}