【问题标题】:HttpURLconnection connection failure with non .com website与非 .com 网站的 HttpURLconnection 连接失败
【发布时间】:2019-10-18 10:17:01
【问题描述】:

我一直在尝试将我的 android 应用程序连接到 php 服务器。我创建了一个 php 服务器 echo "Connection success" 以便我可以检索相同的文本并将其显示在我的 android 应用程序中的 Alertbox 中。我有一个带有.cf 的网站,每次连接时应用程序都会关闭。 我检查了我的代码没有发现任何问题,然后我尝试了一个.com 网站,并且该应用程序成功地能够检索页面上显示的任何内容的 html 表单。那么有什么东西可以阻止HttpURLconnection 表单连接到非@ 987654327@网站。

这里是代码。

public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
    context = ctx;
}//takes context as an argument
@Override
protected String doInBackground(String... params) {
    String type = params[0];
    String login_url = "http://www.thejoint.cf/test.php";
    if(type.equals("login")) {
        try {
            String user_name = params[1];
            String password = params[2];
            URL url = new URL(login_url);//what does the url object have
            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();
            bufferedWriter.close();
            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);
    Log.i("info",result);
    alertDialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

}

【问题讨论】:

  • " 并且每次连接时应用程序都会关闭" 是否有任何错误或异常?如果有任何问题,请将它们发布在问题中。

标签: java android hosting


【解决方案1】:

那么有什么东西会阻止 HttpURLconnection 表单连接到非 .com 网站吗?

你创建一个正则表达式:

String expression  =  "[^"]+\.com" ; 

并测试您的网址是否与之匹配:

if(your_url.matches(expression)){

 // your url end with .com , do what you want 

}else{

 // your url doesn't end with .com , do what you want

}

【讨论】:

    猜你喜欢
    • 2012-12-22
    • 2020-11-26
    • 2014-10-15
    • 1970-01-01
    • 2016-04-10
    • 2018-06-22
    • 2014-02-24
    • 1970-01-01
    • 2012-01-20
    相关资源
    最近更新 更多