Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 受移动网络提供商影响的Android HttpUrlConnection网络代码_Java_Android_Mobile_Android Volley_Httpurlconnection - Fatal编程技术网

Java 受移动网络提供商影响的Android HttpUrlConnection网络代码

Java 受移动网络提供商影响的Android HttpUrlConnection网络代码,java,android,mobile,android-volley,httpurlconnection,Java,Android,Mobile,Android Volley,Httpurlconnection,我怀疑我的移动网络提供商正在影响我的代码在设备上的工作方式 最初,我编写了google截取代码,向服务器发送一个配置文件图像,其中包含图像所有者的详细信息 这段代码已经无缝工作了很长一段时间,直到我在LG Optimus G Pro上运行它,发现它在大图像上悄无声息地失败。没有例外或错误。它适用于小图像(约94KB及以下) 我在三星Galaxy S5上测试了该代码,它对大小图像都很有效。然后,我开始怀疑截击密码,在这种情况下,似乎没有任何正当理由 因此,我将代码移植到HttpUrlConnect

我怀疑我的移动网络提供商正在影响我的代码在设备上的工作方式

最初,我编写了google截取代码,向服务器发送一个配置文件图像,其中包含图像所有者的详细信息

这段代码已经无缝工作了很长一段时间,直到我在LG Optimus G Pro上运行它,发现它在大图像上悄无声息地失败。没有例外或错误。它适用于小图像(约94KB及以下)

我在三星Galaxy S5上测试了该代码,它对大小图像都很有效。然后,我开始怀疑截击密码,在这种情况下,似乎没有任何正当理由

因此,我将代码移植到HttpUrlConnection,并使用我在下面创建的类
HttpUploader

令我恐惧的是,我得到了与截击完全相同的结果

但是,我现在注意到,当它失败时,服务器仍然返回
200
的响应代码,并且写入图像字节的循环仍然完成

下一个嫌疑人是网络提供商,比如说ETS

我使用MTN Nigeria将有问题的设备连接到Galaxy S5的无线热点,这一次,代码成功了

我现在使用ETS将Galaxy S5连接到LG Optimus Pro的无线热点,瞧,Galaxy S5也出现了同样的问题

因此我证实了我的怀疑,即ETS网络正在窃听代码

以下是HttpUrlConnection代码:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by hp on 1/18/2016.
 */
public abstract class HttpUploader {

    /**
     * This is important for uploading multipart data containing a large file.
     * @param serverUrl The server url
     * @param f The file to be uploaded
     * @param params The parameters of text data to be uploaded
     * @param values The values of text  data to be uploaded
     * @return the server's response
     */
    public void uploadStuff(final String serverUrl,final int retries,final File f,final String[]params,final String...values){
   new Thread(new Runnable() {
       @Override
       public void run() {
onPreStart();

        if( params==null || values==null || serverUrl==null || params.length!=values.length){
            return;
        }
        String response = "error";
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;

        String urlServer = serverUrl;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024;
        try {
            FileInputStream fileInputStream = new FileInputStream( f );

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setChunkedStreamingMode(maxBufferSize);
            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type",
                    "multipart/form-data; boundary=" + boundary);

            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);

            for(int i=0;i<params.length;i++){
                String param = params[i];
                String value = values[i];

                String token = "token-"+i;
                outputStream.writeBytes("Content-Disposition: form-data; name=\""+param+"\"" + lineEnd);
                outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
                outputStream.writeBytes("Content-Length: " + token.length() + lineEnd);
                outputStream.writeBytes(lineEnd);
                outputStream.writeBytes(value + lineEnd);
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            }//end or loop


            String connstr = "Content-Disposition: form-data; name=\"file\";filename=\""
                    + f.getAbsolutePath() + "\"" + lineEnd;

            outputStream.writeBytes(connstr);
            outputStream.writeBytes(lineEnd);

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

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            try {
                while (bytesRead > 0) {
                    try {
                        outputStream.write(buffer, 0, bufferSize);

                    } catch (OutOfMemoryError e) {
                        e.printStackTrace();
                        Utils.logErrorMessage("outofmemoryerror");
                        response = "outofmemoryerror";
                    }
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }
            } catch (Exception e) {
                e.printStackTrace();
                response = "error";
                Utils.logErrorMessage("exception: "+e.getMessage());
            }
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                    + lineEnd);

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


            if (serverResponseCode == 200) {
                response = "true";
            }
            else{

             if(retries!=0) {
                 uploadStuff(serverUrl, retries - 1, f, params, values);
             }
                  response = "false";
            }

            fileInputStream.close();
            outputStream.flush();

            //for android InputStream is = connection.getInputStream();
            java.io.InputStream is = connection.getInputStream();

            int ch;
            StringBuilder b = new StringBuilder();
            while( ( ch = is.read() ) != -1 ){
                b.append( (char)ch );
            }

            String responseString = b.toString();
            if(serverResponseCode == 200){
                onSuccess(responseString);
            }
            else{
                onError(responseString);
            }
            Utils.logErrorMessage("response string is " + responseString); //Here is the actual output

            outputStream.close();
            outputStream = null;

        } catch (Exception ex) {
            // Exception handling
            response = "error";

            ex.printStackTrace();
        }


       }
   }).start();
    }





    public abstract void onPreStart();
    public abstract void onSuccess(String response);

    public abstract void onError(String response);


}
有什么办法可以纠正这个问题吗

HttpUrlConnection公开了代码找到了服务器,一切正常,但它根本没有将图像上传到服务器,这让我感到震惊

我在有问题的网络上使用WhatsApp,它可以上传任何大小的图片。我可以使用什么黑客或变通方法来挽救这种局面?
谢谢。

请告诉我们有关您的错误的详细信息?当它失败时,上传图像时?是的,我在问题中至少提到过两次。该代码旨在将配置文件映像和有关映像所有者的文本数据发送到服务器。代码中的所有内容都正常完成,但根本无法将数据传输到服务器。您认为这是网络连接问题吗?连接速度慢、超时等。任务完成前多久?嗯,也许我应该检查一下……不过,令人不快的网络比在尼日利亚工作的网络有“更快”的名声
        new HttpUploader() {
            @Override
            public void onPreStart() {}
            @Override
            public void onSuccess(final String response) {}
            @Override
            public void onError(final String response) {}
        }.uploadStuff(url, 5, imageSrc, new String[]{"key", "creator_name", "user_id", "user_phone"}, "file", Utils.owner.getFullName(), Utils.owner.getId(), Utils.owner.getFullPhoneNumber(),
                );