Android-Client没有得到java-server完整代码的答案

时间:2017-07-08 15:03:40

标签: java android

我的socketapplikation有问题。 我试图在java serversocket和Android客户端之间更改字符串。 服务器从客户端获取字符串,但客户端没有从服务器接收答案。如果我调试应用程序,它会从input.readline()直接跳转到ioexception ...服务器在我的笔记本电脑上的eclipse中运行,而在我的塔式PC上的emulator上运行android-app。

这是我的Android客户端。

 package com.example.user.clientsocket;


         import java.io.BufferedReader;
         import java.io.BufferedWriter;
         import java.io.IOException;
         import java.io.InputStreamReader;
         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.graphics.Color;
         import android.os.Bundle;
         import android.view.View;
         import android.widget.EditText;
         import android.widget.TextView;

 public class TheClient extends Activity {

     private Socket socket;
     private static final int SERVERPORT = 1343;
     private static final String SERVER_IP = "192.168.0.102";
     //private BufferedReader input;

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_the_client);
         new Thread(new ClientThread()).start();
     }

     public void onClick(View view) {     
         try {  
             EditText et =  findViewById(R.id.EditText01);
             String str = et.getText().toString();
             PrintWriter out = new PrintWriter(new BufferedWriter(
                     new OutputStreamWriter(socket.getOutputStream())),
                     true);

             BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             String read = input.readLine();
             out.println(str);
             out.flush();
             out.close();          
             //Input


             //BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

             //String read = ("da website");
             TextView url = findViewById(R.id.url);
             url.setText(read);
             //Input
             socket.close();     
         } catch (UnknownHostException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }

     class ClientThread implements Runnable {
         @Override
         public void run() { 
             try {
                 InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                 socket = new Socket(serverAddr, SERVERPORT);
             } catch (UnknownHostException e1) {
                 e1.printStackTrace();
             } catch (IOException e1) {
                 e1.printStackTrace();
             }
         }
     } 
}

这就是Manifest

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


     <uses-permission android:name="android.permission.INTERNET" >
     </uses-permission>
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
     </uses-permission>


     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
         <activity android:name=".TheClient">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

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

 </manifest>

至少这是我的Java-Server

public class WinCCSocket
{
    public static void main(String args[]) throws IOException
    {
        String URL="https://10.11.226.7/";

        String number, temp;
        ServerSocket s1 = new ServerSocket(1343);
        Socket ss = s1.accept();
        Scanner sc = new Scanner(ss.getInputStream());
        number = sc.nextLine();

        temp = number;
        System.out.println(temp);

        PrintStream p = new PrintStream(ss.getOutputStream());
        p.println(URL);
        p.flush();
        p.close();

        s1.close();
        sc.close();
        xml_write(temp);                        
    }

    public static void xml_write(String temp)
    {
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            Element rootElement = doc.createElement("Liste");
            doc.appendChild(rootElement);

         // letter
            Element Eintrag = doc.createElement("Eintrag");
            rootElement.appendChild(Eintrag);

            // name 
            Element Telefon = doc.createElement("Telefon");
            Telefon.appendChild(doc.createTextNode(temp));
            Eintrag.appendChild(Telefon);

            /*// street
            Element street = doc.createElement("street");
            street.appendChild(doc.createTextNode(temp));
            letter.appendChild(street);*/

            // als XML schreiben
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            //Wincc vm folder
            StreamResult result = new StreamResult(new File("C:/Users/Public/Documents/stringtest.xml")); 
            //testfolder local
            //StreamResult result = new StreamResult(new File("F:/example.xml"));  
            //WinCC VM location
            //StreamResult result = new StreamResult(new File("C:/Users/Public/Documents/Siemens/WinCCProjects/DasProjekt/NFCCode.xml"));

            transformer.transform(source, result);

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (TransformerException tfe) {
            tfe.printStackTrace();
        }                   
    }                       
}

0 个答案:

没有答案