dr9379 2019-04-14 09:25
浏览 1253
已采纳

意外的状态行:在HttpURLConnection上使用getInputStream()时的Y������

I am trying to connect to a mysql database using an HTTP request. I have created a class which extends AsyncTask ad takes care of making the http post request and retrieving the data. Here's my code:

public class AttemptLogin extends AsyncTask {

        String mUserName;
        String mPassword;

        String un = "admin01";
        String pass = "admin01";
        Connection con;
        Context context;

        AlertDialog dialog ;

        public AttemptLogin(Context context, String mUserName, String Password) {
            this.context   = context;
            this.mUserName = mUserName;
            this.mPassword = Password;
        }

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

        protected  void onPostExecute(String s){
            dialog.setMessage(s);
            dialog.show();
        }

        @Override
        protected Object doInBackground(Object[] objects) {

            try{

                Log.d(tag, "Debut do in background");
                String constr = "http://000.000.0.00:3306/login.php";
                URL url = new URL(constr);
                HttpURLConnection http = (HttpURLConnection)url.openConnection();
                http.setDoInput(true);
                http.setDoOutput(true);
                http.setUseCaches(false);
                http.setRequestMethod("POST");
                http.setRequestProperty("Connection", "close");

                OutputStream ops = http.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops,"UTF-8"));

                String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(mUserName, "UTF-8") + " & " +
                              URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(mPassword, "UTF-8");

                writer.write(data);
                writer.flush();
                writer.close();
                ops.close();

                String result = "";
                String line = "";

                InputStream ips = http.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(ips,"ISO-8859-1"));

                while ((line = reader.readLine()) != null){
                    result += line;
                }

                Log.d(tag, "Result = " + result);

                reader.close();
                ips.close();
                http.disconnect();

                onPostExecute(result);

                return result;

            }
            catch (Exception e) {
                Log.w("Error connection","" + e.getMessage());
                e.printStackTrace();
            }

            return null;

        }
    }

Of course, the IP I've placed is just to hide my real IP. Anyway, the line at getInputStream() is returning the following error: Unexpected status line: Y������

I've tried many things incuding setting the "Connection" property to "close" but it didn't work out.

Any help would be appreciated as I've been stuck for some time now. Thank you!

  • 写回答

1条回答 默认 最新

  • dpp78272 2019-04-14 11:14
    关注

    It seems you are trying to make an HTTP request to a port which runs an MySQL-specific protocol. The request happens, but the response is not an HTTP response, thus the exception.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料