如何解决Android应用程序的服务器响应问题

时间:2017-11-17 19:16:32

标签: java android server dns xampp

当我将android应用程序重定向到我的本地xampp服务器时,我得到了服务器的预期输出。如下所示

enter image description here

问题是当我将我的应用程序重定向到真正的IP或域服务器时出现问题。这是我的代码。

 protected String doInBackground(String[] paramparameterForURL) {

        try{
            //serv_url="http://www.eurekabd.com";//shakil/"+paramparameterForURL[0];
            URL url = new URL("http://www.eurekabd.com/shakil/home.php"/*serv_url*/);
            //URL url = new URL("http://192.168.0.109/shakil/shakil.php"/*serv_url*/);
            //URL url = new URL("http://144.48.2.11/shakil/shakil.php"/*serv_url*/);
            JSONObject postDataParams = new JSONObject();

            postDataParams.put("name", "abhay");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(300 /* milliseconds */);
            conn.setConnectTimeout(300 /* milliseconds */);
            //conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));
            writer.flush();
            writer.close();
            os.close();

            int responseCode=conn.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuffer sb = new StringBuffer("");
                String line="";

                while((line = in.readLine()) != null) {
                    sb.append(line);

                    break;
                }
                in.close();
                return sb.toString();
            }
            else {
                return new String("false : "+responseCode);
            }
        }
        catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }
    }

问题

1.Server WWW.eurecabd.com返回异常NULL,如下所示 enter image description here

2.real ip server像空格一样返回空白

enter image description here

如何解决问题或者问题是什么?是编码还是网络协议?

1 个答案:

答案 0 :(得分:1)

问题是不同服务器的响应,你也应该修改它:

conn.setConnectTimeout(300 /* milliseconds */);

300毫秒对于连接超时来说太低了,记住是以毫秒为单位。