有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java连接在一个屏幕上成功下载,在第二个屏幕上用几乎相同的代码获得错误

当我试图从两个具有相同连接代码的不同屏幕下载同一php文件中的“成功”消息时,我遇到了一个奇怪的事件。每个页面都有一个按钮来连接并返回success以及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();
        }}}}

共 (1) 个答案

  1. # 1 楼答案

    查看错误消息,尤其是FailedURI:

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

    您正试图点击http://sinfo.txt/test.php。由于该主机名显然不存在,您将获得ISP的dns重定向页面

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