如何将ApiKey和令牌添加到HttpURLConnection

时间:2018-08-07 12:38:21

标签: android

我实现了一个上载图像的AsyncTask类。如何将ApiKey和Token添加到HttpURLConnection实例?

Voila代码:

@SuppressLint("StaticFieldLeak")
class UploadFileTask extends AsyncTask<String, Void, String> {

    @SuppressLint("LongLogTag")
    protected String doInBackground(String... urls) {
        try {

            HttpURLConnection conn;

// ...

                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true); 
                    conn.setDoOutput(true); 
                    conn.setUseCaches(false); 
                    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", sourceFileUri);

// ...

1 个答案:

答案 0 :(得分:1)

这是完整的示例

public class SendPostRequest extends AsyncTask<String, Void, String> {
        protected void onPreExecute() {

        }

        protected String doInBackground(String... arg0) {

            try {
                URL url = new URL("URL HERE");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                conn.setRequestProperty("Accept", "application/json");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setReadTimeout(120000);
                conn.setConnectTimeout(120000);
                JSONObject postDataParams = new JSONObject();
                postDataParams.put("api_key", value_here);
                postDataParams.put("token", value_here);
                Log.i("JSON", postDataParams.toString());
                DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
                os.writeBytes(postDataParams.toString());
                os.flush();
                os.close();

                Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                Log.i("MSG", conn.getResponseMessage());


                int responseCode = conn.getResponseCode();

                if (responseCode == HttpsURLConnection.HTTP_OK) {

                    BufferedReader in = new BufferedReader(new
                            InputStreamReader(
                            conn.getInputStream()));

                    StringBuffer sb = new StringBuffer("");
                    String line = "";

                    while ((line = in.readLine()) != null) {

                        sb.append(line);
                        break;
                    }

                    in.close();
                    conn.disconnect();
                    return sb.toString();

                } else {
                    conn.disconnect();
                    return new String("false : " + responseCode);
                }

            } catch (Exception e) {
                return new String("Exception: " + e.getMessage());
            }

        }

        @Override
        protected void onPostExecute(String result) {
            try {
                JSONObject obj = new JSONObject(result);
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    }