HTTP POST请求问题(Android Volley)

时间:2016-09-05 13:03:53

标签: java android android-studio android-volley

我按照此tutorial将图片从手机发送到服务器,但图片从不发送。事实上,应用程序甚至不会尝试发送图像(在logcat中)。我在logcat中看到的只有:

编辑2:

我在" requestQueue(request)"之后添加了这段代码。并且映像已成功发送到服务器,但 image_name 由于某种原因未发送到服务器。服务器上的文件只是" .jpg",没有文件名。

int x=1;// retry count
    request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,
            x, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}

编辑后的LOGCAT:

09-06 20:59:04.530 4188-4194/com.example.android.projectlanguage W/art: Suspending all threads took: 19.394ms
09-06 20:59:10.752 4188-4198/com.example.android.projectlanguage I/art: Background partial concurrent mark sweep GC freed 344902(7MB) AllocSpace objects, 3(39MB) LOS objects, 20% free, 63MB/79MB, paused 1.815ms total 112.441ms
09-06 20:59:16.046 4188-4194/com.example.android.projectlanguage W/art: Suspending all threads took: 7.118ms
09-06 20:59:21.501 4188-9226/com.example.android.projectlanguage D/Volley: [1453] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://128.199.77.211/server/connection.php 0x9c36b58 NORMAL 1> [lifetime=14541], [size=314], [rc=200], [retryCount=0]

的AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.hardware.camera"
    android:required="true" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

MainActivity.java

public class MainActivity extends AppCompatActivity {

private ImageButton ImageButton;
private String encoded_string, image_name;
private Bitmap bitmap;
private File file;
private Uri file_uri;


// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageButton = (ImageButton) findViewById(R.id.camera);
    ImageButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            getFileUri();
            i.putExtra(MediaStore.EXTRA_OUTPUT,file_uri);
            startActivityForResult(i,10);
        }
    });
}

private void getFileUri() {
    image_name = "testing123.jpeg";
    file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), image_name);

    file_uri = Uri.fromFile(file);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == 10 && resultCode == RESULT_OK){
        new Encode_image().execute();
    }
}

private class Encode_image extends AsyncTask<Void,Void,Void> {
   @Override
   protected Void doInBackground(Void... voids){

       bitmap = BitmapFactory.decodeFile(file_uri.getPath());
       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);

       byte[] array = stream.toByteArray();
       encoded_string = Base64.encodeToString(array,0);
       return null;
   }

   @Override
   protected void onPostExecute(Void aVoid){
       makeRequest();
   }
}

private void makeRequest() {
    final TextView mTextView = (TextView) findViewById(R.id.text);
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    String URL = "http://128.199.77.211/server/connection.php";
    StringRequest request = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    mTextView.setText(response);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            mTextView.setText("That didn't work!");
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> map = new HashMap<>();
            map.put("encoded_string", encoded_string);
            map.put("image_name", image_name);

            return map;
        }
    };
    requestQueue.add(request);
  }
}

我已确保在build.gradle中包含volley库。

然而,排球确实将文字设置为&#34;那不起作用!&#34;

编辑:

添加&#34; Log.e(&#34; TAG&#34;,&#34;错误&#34;,错误);&#34;到onErrorResponse,这就是Logcat产生的:

09-05 23:21:44.593 18677-18677/com.example.android.projectlanguage E/TAG: err
                                                                      com.android.volley.TimeoutError
                                                                          at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:147)
                                                                          at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

1 个答案:

答案 0 :(得分:0)

嘿,这是可以帮助你尝试的课程。

使用

如果您没有通过SSL发送请求,请

connection = url.openConnection();

public class MultipartRequest extends AsyncTask<Void, Integer, String> {

    /**
     * Url for uploading form data to server
     */
    private String serverUrl;

    /**
     * Url for maintaining body paramters
     */
    private HashMap<String, String> bodyParams;

    /**
     * File for uploading
     */
    private File file;

    //Connection Object
    private HttpURLConnection connection = null;

    //Data output stream

    private DataOutputStream outputStream = null;

    private DataInputStream inputStream;

    //Content managers
    private String lineEnd = "\r\n";
    private String twoHyphens = "--";
    private String boundary = "*****";

    //Response callback for request
    private ResponseCallback responseCallback;

    //Response string
    private String responseString = null;

    /**
     * Constructor for multipart request
     *
     * @param url
     * @param bodyParams
     * @param file
     */
    public MultipartRequest(String url, HashMap<String, String> bodyParams,
                            File file, ResponseCallback responseCallback){
        this.serverUrl = url;
        this.bodyParams = bodyParams;
        this.file = file;
        this.responseCallback = responseCallback;
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            URL url = new URL(serverUrl);
            connection = (HttpsURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            connection.addRequestProperty("Authorization", StorageUtility.getBasicAuth());
            outputStream = new DataOutputStream(connection.getOutputStream());

            // attach body params
            attachBodyParams(outputStream);

            if(file != null) {
                attachFileParams(outputStream);
            }

            System.out.println("+++++++++++++++++++++++"+twoHyphens + boundary + twoHyphens + lineEnd);

            // write bytes
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);


            inputStream = new DataInputStream(connection.getInputStream());
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = inputStream.readLine()) != null) {
                response.append(line).append('\n');
            }

            responseString = response.toString();
        }catch (ProtocolException pe){
            pe.printStackTrace();
        }catch (IOException io){
            io.printStackTrace();
        }

        return null;
    }

    /**
     * Attach body params
     * @param outputStream
     * @throws IOException
     */
    public void attachBodyParams(DataOutputStream outputStream) throws IOException{
        for(String key : bodyParams.keySet()) {
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\" " + key + "\"");
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(bodyParams.get(key));
            outputStream.writeBytes(lineEnd);
        }
    }

    /**
     * if there is file attach file at the end of
     * body
     *
     * @param outputStream
     * @throws IOException
     */
    public void attachFileParams(DataOutputStream outputStream) throws IOException{

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;

        FileInputStream fileInputStream = new FileInputStream(file);

        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"");
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes("Content-Type: application/*");
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(lineEnd);

        //hier File schreiben
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0) {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        outputStream.writeBytes(lineEnd);

        fileInputStream.close();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        try {
            responseCallback.onResponse(new JSONObject(responseString.toString()));
        }catch (JSONException je){
            je.printStackTrace();
            logger.error(StackTraceUtility.getStackTrace(je));
        } catch (NullPointerException ne){
            ne.printStackTrace();
            logger.error((StackTraceUtility.getStackTrace(ne)));
        }
    }
}

并且只是用

创造意图
new MultipartRequest(url, bodyParams,
                       file, responseCallback)