php - 如何将 Echo 消息从 php 转换为字符串进行比较?

标签 php android mysql

我想将从网络服务器接收到的回显消息转换为字符串,我想将其与onPostExecute中的另一个字符串进行比较 方法。 当输入正确的值时,仅显示 onPostExecute 的 else 部分。为什么这样?我应该怎么做才能解决这个问题?

@Override
public String doInBackground(String... params) {

    //String reg_url="http://127.0.0.1//webapp/Register.php";
        String reg_url="http://10.0.2.2/webapp/Register.php";
        String login_url="http://10.0.2.2/webapp/login.php";
        String method= params[0];
        if(method.equals("register"))
        {
            String name=params[1];
            String u_address=params[2];
            String user_pass=params[3];
            String u_email=params[4];
            String u_zip=params[5];
            String u_mob=params[6];

            try {
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
                String data = URLEncoder.encode("user","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
                        URLEncoder.encode("address","UTF-8")+"="+URLEncoder.encode(u_address,"UTF-8")+"&"+
                        URLEncoder.encode("user_pass","UTF-8")+"="+URLEncoder.encode(user_pass,"UTF-8")+"&"+
                        URLEncoder.encode("emailid","UTF-8")+"="+URLEncoder.encode(u_email,"UTF-8")+"&"+
                        URLEncoder.encode("zip","UTF-8")+"="+URLEncoder.encode(u_zip,"UTF-8")+"&"+
                        URLEncoder.encode("mob","UTF-8")+"="+URLEncoder.encode(u_mob,"UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
               BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

                String result;
                result = bufferedReader.readLine();
               // / InputStream is= httpURLConnection.getInputStream();
               // is.close();
               // return "Registration Success...";
                return result.toString();
            }
            catch(Exception e)
            {
                return null;
            }
            /*catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }*/
        }

    else {
            if (method.equals("login")) {
                String login_name = params[1];
                String login_pass = params[2];
                try {
                    URL url = new URL(login_url);
                    HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream os = httpURLConnection.getOutputStream();
                    BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
                    String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
                            URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");

                    bufferedWriter.write(data);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    os.close();

                    InputStream inputStream=httpURLConnection.getInputStream();
                    BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                    String response="";
                    String line="";
                    while ((line=bufferedReader.readLine())!=null)
                    {
                        response+=line;
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    return response.toString();

                }
               /* catch(Exception e)
                {
                    return null;
                } */
                catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    return null;
}

我正在尝试比较从服务器返回的回显响应,该响应又返回到 onPostExecute 方法,如下所示

 protected void onPostExecute(String s) {
   /* if(result.equals("Registration Success...")){
        Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
    }
    else{
    //if(result.equals("Login Success...")){
        //  Toast.makeText(ctx,"Login Success!",Toast.LENGTH_LONG).show();
        alertDialog.setMessage(result);
        alertDialog.show();
    }
    /*else
    {
        Toast.makeText(ctx,"Failed!",Toast.LENGTH_LONG).show();
    } */

   /* if(method.equals("login"))
    { */
    loading.dismiss();
        if(s.equals("Login Success..."))
        {

            Intent intent = new Intent(ctx,MainActivity.class);
          //  intent.putExtra("USER_NAME",login_name);
            ctx.startActivity(intent);

        }


   /*     else{
            Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
        }
    }
    else if(method.equals("register")) {
   */
        else if (s.equals("Registered Successfully")){
            Intent intent = new Intent(ctx,Login.class);
            ctx.startActivity(intent);
        }
        else{
            Toast.makeText(ctx,s,Toast.LENGTH_LONG).show();
        }
   // }
}

最佳答案

确保回显消息位于上面的代码中。 我建议创建一个字符串变量并将其等同于回显消息。喜欢:

字符串 serverResponseText = new String(serveResponse.toString());

这应该可以解决你的问题。祝你好运

关于php - 如何将 Echo 消息从 php 转换为字符串进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36232431/

相关文章:

php - 制作N个字符的字符串

java - 如何使android日期选择器不允许选择过去的日期

android - 实现 MVP 时,在 Android 中保留 Presenter 的最佳做法是什么?

python - 尝试使用 Peewee 输入 ForeignKey 字段时出现 AttributeError

javascript - 数据表列过滤器未显示

php - html 表单和 url 中的 Laravel 嵌套资源路由参数

android - 如何将项目从另一个类的列表添加到 ListView

mysql - 在 MySQL 过程中获取 SQLEXCEPTION 消息

php - 为什么 phpMyAdmin 没有导入我的表?

php - 无法连接到远程 MySQL 服务器(使用默认服务器代替)