简体   繁体   English

如何在 Koush ion 中构建 URL

[英]How to build a URL in Koush ion

I am building an app to upload images to my company server I am using koush-ion for the upload, now my problem is I have to dynamically change the URL for upload depending on information entered in another activity(LoginActivity) via edittext boxes and I don't understand how to do that so what I want to happen is the client enters thier Email, password and clientID(4 digits)(in LoginActivity) and the app uses that to build a url for the upload like this one(in CameraActivity) https://www.blahpractice.co.za/files-upload-ruben.asp?MyForm=Yes&ClientID=1234&Username=man@blahpractice.co.za&Pwd=BlahBlah123@我正在构建一个应用程序以将图像上传到我的公司服务器我正在使用 koush-ion 进行上传,现在我的问题是我必须根据通过编辑文本框在另一个活动(登录活动)中输入的信息动态更改上传的 URL,我不明白该怎么做,所以我想要发生的是客户端输入他们的电子邮件、密码和 clientID(4 位数字)(在 LoginActivity 中),应用程序使用它来构建一个像这样上传的 url(在 CameraActivity 中) ) https://www.blahpractice.co.za/files-upload-ruben.asp?MyForm=Yes&ClientID=1234&Username=man@blahpractice.co.za&Pwd=BlahBlah123@

I got this From the koush github, and i am unsure if this is what i am looking for and also how to implement data from another activity in koush-ion我从 koush github 得到了这个,我不确定这是否是我正在寻找的,以及如何从 koush-ion 中的另一个活动实现数据

Post application/x-www-form-urlencoded and read a String发布应用程序/x-www-form-urlencoded 并读取一个字符串

 Ion.with(getContext())
   .load("https://koush.clockworkmod.com/test/echo")
   .setBodyParameter("goop", "noop")
   .setBodyParameter("foo", "bar")
   .asString()
   .setCallback(...)

Camera Activity相机活动

  public class CameraActivity extends AppCompatActivity implements 
   View.OnClickListener{
    private final int PICK_IMAGE = 12345;
    private final int REQUEST_CAMERA = 6352;
    private static final int REQUEST_CAMERA_ACCESS_PERMISSION =5674;
    private Bitmap bitmap;

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        imageView =findViewById(R.id.imageView);
        Button fromCamera=findViewById(R.id.fromCamera);
        Button fromGallery=findViewById(R.id.fromGallery);
        Button upload=findViewById(R.id.upload);
        upload.setOnClickListener(this);
        fromCamera.setOnClickListener(this);
        fromGallery.setOnClickListener(this);


        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
            fromCamera.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.fromCamera:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                        && ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.CAMERA},
                            REQUEST_CAMERA_ACCESS_PERMISSION);
                }else {
                    getImageFromCamera();
                }
                break;
            case R.id.fromGallery:
                getImageFromGallery();
                break;
            case R.id.upload:
                if (bitmap != null)
                    uploadImageToServer();
                break;
        }

    }


    private void uploadImageToServer() {

I want to call the url here我想在这里调用网址

  File imageFile = persistImage(bitmap, "SP_Upload");
        Ion.with(this)
                .load("https://www.Blahpractice.co.za/files-upload-ruben.asp?MyForm=Yes")
                .setMultipartFile("SP-LOG", "image/jpeg", imageFile)
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {

                    }
                });
    }

    private File persistImage(Bitmap bitmap, String name) {
        File filesDir = getApplicationContext().getFilesDir();
        File imageFile = new File(filesDir, name + ".jpg");

        OutputStream os;
        try {
            os = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
            os.flush();
            os.close();
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
        }

        return imageFile;
    }


    private void getImageFromCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQUEST_CAMERA);
    }


    private void getImageFromGallery() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE) {
            if (resultCode == Activity.RESULT_OK) {
                try {
                    InputStream inputStream = getContentResolver().openInputStream(data.getData());
                    bitmap = BitmapFactory.decodeStream(inputStream);
                    imageView.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

            }
        } else if (requestCode == REQUEST_CAMERA) {
            if (resultCode == Activity.RESULT_OK) {
                Bundle extras = data.getExtras();
                bitmap = (Bitmap) extras.get("data");
                imageView.setImageBitmap(bitmap);
            }
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CAMERA_ACCESS_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getImageFromCamera();
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

LoginActivity登录活动

     public class LoginActivity extends AppCompatActivity {

    private EditText email, password, id;

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

        email=findViewById(R.id.emailtext);
        password=findViewById(R.id.pwdtext);
        id=findViewById(R.id.clientid);
        Button loginBtn=findViewById(R.id.button);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String emailAddress=email.getText().toString().trim();
                String userPassword=password.getText().toString().trim();
                String clientId=id.getText().toString().trim();
                Intent intent=new Intent(LoginActivity.this, CameraActivity.class);
                intent.putExtra("clientId", clientId);
                intent.putExtra("email", emailAddress);
                intent.putExtra("password", userPassword);
                startActivity(intent);
            }
        });
    }
}

You could store the url on the shared preferences and retrieve it every time you execute your upload task and set it on the .load() method.您可以将 url 存储在共享首选项中,并在每次执行上传任务时检索它,并将其设置在 .load() 方法上。

Also, what you need to send an image to your server is a multipart post.此外,您需要将图像发送到您的服务器是一个多部分的帖子。 I haven't used Ion but I have used multipart in other libraries like OkHttp, I´ve copied the method that appears on the Ion documentation:我没有使用过 Ion,但我在 OkHttp 等其他库中使用了 multipart,我复制了 Ion 文档中出现的方法:

String dynamicUrl = PreferenceManager.getDefaultSharedPreferences(context).getString(CURRENT_SELECTED_URL, null);
File myImage = new File(myImagePath);

if(dynamicUrl != null) {
    Ion.with(getContext())
    .load(dynamicUrl)
    .uploadProgressBar(uploadProgressBar)
    .setMultipartParameter("goop", "noop")
    .setMultipartFile("myImageName", "image/*", myImage)
    .asJsonObject()
    .setCallback(...)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM