java—在android应用程序客户端上从python服务器接收值,然后显示与该值对应的mysql数据

pbpqsu0x  于 2021-06-19  发布在  Mysql
关注(0)|答案(0)|浏览(252)

我将从android客户端上的python服务器接收数据,并将数据存储在一个名为answerfromserver的变量中。之后,我使用answerfromserver从wamp数据库检索数据。现在,当我在手机上安装这个应用程序时,会发生这样的情况:当我从wamp获取正确的数据以便检索时,我的应用程序崩溃了。我正在附上我的应用程序代码,请帮我。。
编辑-我注解掉了从mysql wamp服务器接收数据的代码的执行,因此在这种情况下,我的应用程序正确地显示了来自python服务器的数据,但是注解掉了python服务器代码应用程序崩溃的执行。。

package marecki.androidsocketclient;

    import android.content.Context;
    import android.os.AsyncTask;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.view.View.OnClickListener;
    import android.widget.Toast;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.Socket;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.net.UnknownHostException;

    import static java.io.DataInputStream.readUTF;

    public class MainActivity extends AppCompatActivity {

        TextView textAnswer;
        Button buttonConnect;
        EditText editTextIP, editTextPort, message;
        ProgressBar progressBar;
        String result="";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            textAnswer = (TextView)findViewById(R.id.text_answer);
            buttonConnect = (Button)findViewById(R.id.connect);
            editTextIP = (EditText)findViewById(R.id.address);
            editTextPort = (EditText)findViewById(R.id.port);
            message = (EditText)findViewById(R.id.edit_message);

           progressBar = (ProgressBar)findViewById((R.id.progressBar));
           progressBar.setVisibility(ProgressBar.INVISIBLE);

            buttonConnect.setOnClickListener(buttonConnectOnClickListener);
        }

        OnClickListener buttonConnectOnClickListener = new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                String Msg = message.getText().toString();

                ClientTask clientTask = new ClientTask(
                                                editTextIP.getText().toString(),
                                                Integer.parseInt(editTextPort
                                                                .getText().toString()),
                                                Msg);
                clientTask.execute();
            }
        };
        public class ClientTask extends AsyncTask <Void, Void, Void> {

            String IPaddress;
            int portAddress;
            String AnswerFromServer ="";
            String msgToServer;

            ClientTask(String address, int port, String msgToServ){
                IPaddress = address;
                portAddress = port;
                msgToServer = msgToServ;
            }

            @Override
            protected void onPreExecute() {
                progressBar.setVisibility(ProgressBar.VISIBLE);
            }

            @Override
            protected Void doInBackground(Void... arg0) {

                Socket socket = null;
                DataOutputStream dataOutputStream = null;
                DataInputStream dataInputStream = null;

                try {
                    socket = new Socket(IPaddress,portAddress);

                    dataOutputStream = new DataOutputStream(
                                                socket.getOutputStream());
                    BufferedReader br = new BufferedReader(
                                                new InputStreamReader(socket.getInputStream()));
                   // dataInputStream = new DataInputStream(
                   //                           socket.getInputStream());

                    if(msgToServer != null){
                        dataOutputStream.writeUTF(msgToServer);
                    }

                    AnswerFromServer = br.readLine();

                    String password = "123";
                    String type = "login";
                    BackgroundWorker backgroundWorker = 
                                new BackgroundWorker(MainActivity.this);
                    backgroundWorker.execute(type, AnswerFromServer, password);

                }catch (UnknownHostException e) {
                    e.printStackTrace();
                    AnswerFromServer = e.toString();
                }catch (IOException e) {
                    e.printStackTrace();
                    AnswerFromServer = e.toString();
                }finally {
                    if(socket != null){
                        try{
                            socket.close();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                }

                if(dataOutputStream != null){
                    try{
                        dataOutputStream.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }

                if(dataInputStream != null){
                    try{
                        dataInputStream.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }

                return null;

            }

            @Override
            protected void onPostExecute(Void aVoid) {
                progressBar.setVisibility(ProgressBar.INVISIBLE);
                textAnswer.setText(AnswerFromServer);
                Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG);
                super.onPostExecute(aVoid);
            }
        }

        public class BackgroundWorker extends AsyncTask<String,Void,String>
        {
            Context context;
            AlertDialog alertDialog;
            BackgroundWorker (Context ctx)
            {

            }

            @Override
            protected String doInBackground(String... params) {
                String type = params[0];
                String login_url="http;//192.168.8.103/login.php";
                if (type.equals("login"))
                {
                    try
                    {
                        String user_name = params[1];
                        String password = params[2];
                        URL url = new URL(login_url);
                        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                        httpURLConnection.setRequestMethod("POST");
                        httpURLConnection.setDoOutput(true);
                        httpURLConnection.setDoInput(true);
                        OutputStream outputStream = httpURLConnection.getOutputStream();
                        BufferedWriter bufferedWriter = 
                                        new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                        String post_data = 
                                        URLEncoder.encode("user_name","UTF-8")
                                                            +"="
                                                            + URLEncoder.encode(user_name,"UTF-8")
                                                            +"&"
                        +URLEncoder.encode("password","UTF-8")+"="+ URLEncoder.encode(password,"UTF-8");
                        bufferedWriter.write(post_data);
                        bufferedWriter.flush();
                        outputStream.close();
                        InputStream inputStream = httpURLConnection.getInputStream();
                        BufferedReader bufferedReader = 
                                        new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                        String result="";
                        String line="";
                        while ((line=bufferedReader.readLine())!=null)
                        {
                            result+=line;

                        }
                        bufferedReader.close();
                        inputStream.close();
                        httpURLConnection.disconnect();
                        return  result;

                    }
                    catch (MalformedURLException e)
                    {
                        e.printStackTrace();

                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();

                    }
                }
                return null;
            }

            @Override
            protected void onPreExecute() {
                alertDialog = new AlertDialog.Builder(context).create();
                alertDialog.setTitle("Login Status");
            }

            @Override
            protected void onPostExecute(String result) {
                alertDialog.setMessage(result);
                alertDialog.show();
            }

        }

    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题