该应用程序可以正常使用实际设备从Internet下载数据,但无法正常工作,但可以通过Bluestacks从Internet下载数据

时间:2018-06-28 05:03:13

标签: android bluestacks

1-我使用bluestacks测试了多个应用程序,并且都可以从Internet下载数据正常运行
2-我在真实设备上测试了我的应用程序,并且可以很好地从Internet下载数据
3-我使用bluestacks测试了我的应用程序,但无法从Internet下载数据

我的应用程序代码似乎有问题,但关键是它仍然可以在真实设备上使用

代码:

public class JSONHttpHandler3 {
    private static final String TAG = JSONHttpHandler3.class.getSimpleName();

    public JSONHttpHandler3() {
    }

    public String makeServiceCall3(String FileNewName,String FileDestination,String sourceFileUri) {

        int serverResponseCode = 0;
        String fileName = FileNewName;
        String upLoadServerUri = null;
        /************* Php script path ****************/
        upLoadServerUri = FileDestination;

        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 5 * 1024 * 1024;
        File sourceFile = new File(sourceFileUri);
        if (!sourceFile.isFile()) {
            Log.e("uploadFile", "Source File not exist :" + sourceFileUri);
            return "0";
        }
        else {

            try {
                FileInputStream fileInputStream = new FileInputStream(sourceFile);

                URL url = new URL(upLoadServerUri);

                // Open a HTTP  connection to  the URL
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true); // Allow Inputs
                conn.setDoOutput(true); // Allow Outputs
                conn.setUseCaches(false); // Don't use a Cached Copy
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                conn.setRequestProperty("uploaded_file", fileName);

                dos = new DataOutputStream(conn.getOutputStream());

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\"; filename=\""
                        + fileName + "\"" + lineEnd);


                int length = fileInputStream.available();

                dos.writeBytes(lineEnd);

                // create a buffer of  maximum size
                bytesAvailable = fileInputStream.available();

                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                // read file and write it into form...
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {

                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                }

                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                // Responses from the server (code and message)
                serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();

                Log.i("BBBuploadFile", "HTTP Response is : "
                        + serverResponseMessage + ": " + serverResponseCode);

                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder total = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null)
                {
                    total.append(line).append('\n');
                }
                Log.d(TAG, "AAAServer Response is: " + total.toString() + ": " + serverResponseCode);

                //close the streams //
                fileInputStream.close();
                dos.flush();
                dos.close();

                if (total.toString().contains("Move successful") == true )
                {
                    Log.e(TAG, "Move To Server successful");
                    return "Move To Server successful";
                }

                else
                {
                    Log.e(TAG, "Move To Server Failed");
                    return "Move To Server Failed";
                }

            } catch (MalformedURLException ex) {

                ex.printStackTrace();
                Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
                return "Move To Server Failed";
            } catch (Exception e) {

                e.printStackTrace();
                Log.e(TAG, "111Exception: " + e.getMessage());
                return "Move To Server Failed";

            }
        }
    } // End else block

为什么?

1 个答案:

答案 0 :(得分:0)

抱歉,问题是在下载过程中检查了先前在真实移动设备中注册的标志,并且由于bluestacks没有注册该标志,因此下载过程出现问题