连接在一个屏幕上成功下载,在第二个屏幕上出现错误,代码几乎相同


Connection successfully downloads on one screen, gets error on second screen with virtually the same code

尝试从具有相同连接代码的两个不同屏幕从同一 php 文件下载"成功"消息时,我遇到了一个奇怪的事件。 每个页面都有一个按钮来连接并返回成功以及 php 文件的信息。 知道为什么会这样吗?连接都成功,在两种情况下都返回 200。

AppInfo返回"成功","购买"返回以下内容:

    11-14 11:23:14.209: D/JWP(2395): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="refresh"
          content="0;url=http://finder.cox.net/main?InterceptSource=0&ClientLocation=us&ParticipantID=96e687opkbv4scrood8k84drs6gw5duf&FailureMode=1&SearchQuery=&FailedURI=http%3A%2F%2Fsinfo.txt%2Ftest.php&AddInType=4&Version=2.1.8-1.90base&Referer=&Implementation=0&method=GET"/>
    <script type="text/javascript">
        url="http://finder.cox.net/main?InterceptSource=0&ClientLocation=us&ParticipantID=96e687opkbv4scrood8k84drs6gw5duf&FailureMode=1&SearchQuery=&FailedURI=http%3A%2F%2Fsinfo.txt%2Ftest.php&AddInType=4&Version=2.1.8-1.90base&Referer=&Implementation=0&method=GET";if(top.location!=location){var w=window,d=document,e=d.documentElement,b=d.body,x=w.innerWidth||e.clientWidth||b.clientWidth,y=w.innerHeight||e.clientHeight||b.clientHeight;url+="&w="+x+"&h="+y;}window.location.replace(url);
    </script>
</head>
<body></body>
</html>

以下是两个页面的代码:

public class AppInfo extends Activity {
    TextView textView;
    public static final String DEBUGTAG = "JWP";
    public static final String DATABASEPASSWORD = "pinfo.txt";
    public static final String SERVERIP = "sinfo.txt";
    public static final String DATABASEUSER = "ninfo.txt";
    public static final String DATABASE = "dinfo.txt";
    public static final String FILESAVED = "filesaved";
    public EditText txtData;
    public EditText dataBase;
    public EditText dataBaseUser;
    public EditText dataBasePassword;
    public EditText serverIp;
    public TextView resultView;
    public String result = "";
    public InputStream isr = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_info);
        serverIp = (EditText) findViewById(R.id.server);
        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        boolean fileSaved = prefs.getBoolean(FILESAVED, true);
        if (fileSaved) {
            loadSavedFile();
            loadSavedFile2();
            loadSavedFile3();
            loadSavedFile4();
        }
        addSaveButtonListener();
        addTestButtonListener();
    }
    // / load saved information from phone
    private void loadSavedFile() {
            }
    private void loadSavedFile2() {
            }
    private void loadSavedFile3() {
            }
    private void loadSavedFile4() {
            }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.app_info, menu);
        return true;
    }
    private void addSaveButtonListener() {
    }
    private void addTestButtonListener() {
        Button test = (Button) findViewById(R.id.testcon);
        test.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new DownloadData().execute("http://"
                        + serverIp.getText().toString() + "/test.php");
                Log.d(MainActivity.DEBUGTAG, "http://"
                        + serverIp.getText().toString() + "/test.php");
            }
        });
    }
    private class DownloadData extends AsyncTask<String, Void, String> {
        String myXmlData;
        protected String doInBackground(String... urls) {
            try {
                myXmlData = downloadXML(urls[0]);
            } catch (IOException e) {
                Log.e("Can not connect Database", e.toString());
                return "Unable to downlad XML file.";
            }
            return "";
        }
        protected void onPostExecute(String result) {
            Log.d("JWP", myXmlData);
            Toast.makeText(AppInfo.this, getString(R.string.toast_success),
                    Toast.LENGTH_LONG).show();
        }
        private String downloadXML(String theUrl) throws IOException {
            int BUFFER_SIZE = 2000;
            InputStream is = null;
            String xmlContents = "";
            try {
                URL url = new URL(theUrl);
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                int response = conn.getResponseCode();
                Log.d("JWP", "The response is " + response);
                is = conn.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                int charRead;
                char[] inputBuffer = new char[BUFFER_SIZE];
                try {
                    while ((charRead = isr.read(inputBuffer)) > 0) {
                        String readString = String.copyValueOf(inputBuffer, 0,
                                charRead);
                        xmlContents = readString;
                        inputBuffer = new char[BUFFER_SIZE];
                    }
                    return xmlContents;
                } catch (IOException e) {
                    Log.e("Can not connect Database", e.toString());
                    e.printStackTrace();
                    return null;
                }
            } finally {
                if (is != null)
                    is.close();
            }
        }
    }
}  

这是购买页面:

public class Buy extends Scan {
public static final String FILESAVED = "filesaved";
public static final String SERVERIP = "sinfo.txt";
public String serverIp;
public String result = "";
public InputStream isr = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buy);
    SharedPreferences prefs = getPreferences(MODE_PRIVATE);
    boolean fileSaved = prefs.getBoolean(FILESAVED, true);
    if (fileSaved) {
        loadSavedFile();
        loadSavedFile2();
    }
    addSendButtonListener();
}
private void loadSavedFile() {
    try {
        FileInputStream fis = openFileInput(VinHolder);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new DataInputStream(fis)));
        TextView editText = (TextView) findViewById(R.id.BUYVIN);
        String line;
        while ((line = reader.readLine()) != null) {
            editText.append(line);
            editText.append("");
        }
        fis.close();
        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(FILESAVED, true);
        editor.commit();
    } catch (Exception e) {
        Log.d(DEBUGTAG, "Can not read file");
    }
}
private void loadSavedFile2() {
    try {
        FileInputStream fis = openFileInput(SERVERIP);
        // BufferedReader reader = new BufferedReader(new InputStreamReader(
        // new DataInputStream(fis)));
        serverIp = SERVERIP.toString();
        fis.close();
        SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(FILESAVED, true);
        editor.commit();
    } catch (Exception e) {
        Log.d(DEBUGTAG, "Can not read file");
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.buy, menu);
    return true;
}
private void addSendButtonListener() {
    Button send = (Button) findViewById(R.id.SEND);
    send.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            new DownloadData().execute("http://" + serverIp.toString()
                    + "/test.php");
            Log.d("JWP", "http://" + serverIp.toString() + "/test.php");
        }
    });
}
private class DownloadData extends AsyncTask<String, Void, String> {
    String myXmlData;
    protected String doInBackground(String... urls) {
        try {
            myXmlData = downloadXML(urls[0]);
        } catch (IOException e) {
            Log.e("Can not connect Database", e.toString());
            return "Unable to downlad XML file.";
        }
        return "";
    }
    protected void onPostExecute(String result) {
        Log.d("JWP", myXmlData);
        Toast.makeText(Buy.this, getString(R.string.toast_success),
                Toast.LENGTH_LONG).show();
    }
    private String downloadXML(String theUrl) throws IOException {
        int BUFFER_SIZE = 2000;
        InputStream is = null;
        String xmlContents = "";
        try {
            URL url = new URL(theUrl);
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            int response = conn.getResponseCode();
            Log.d("JWP", "The response is " + response);
            is = conn.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            int charRead;
            char[] inputBuffer = new char[BUFFER_SIZE];
            try {
                while ((charRead = isr.read(inputBuffer)) > 0) {
                    String readString = String.copyValueOf(inputBuffer, 0,
                            charRead);
                    xmlContents = readString;
                    inputBuffer = new char[BUFFER_SIZE];
                }
                return xmlContents;
            } catch (IOException e) {
                Log.e("Can not connect Database", e.toString());
                e.printStackTrace();
                return null;
            }
        } finally {
            if (is != null)
                is.close();
        }}}}

查看错误消息,特别是 FailedURI:

FailedURI=http%3A%2F%2Fsinfo.txt%2Ftest.php

你想打http://sinfo.txt/test.php.由于该主机名显然不存在,因此您将获得ISP的dns重定向页面。

修复您的代码,然后对您的 ISP 大喊大叫并禁用 DNS 重定向。