不推荐使用BasicHttpParams,UrlEncodedFormEntity

时间:2017-08-11 13:36:11

标签: android

这是我的代码,它显示了我弃用的NameValuePair,Http客户端错误。

有人可以帮助我完成工作吗? 我想从图库中获取图像,并希望将其上传到网络服务器上,但它显示了一些警告并弃用了NameValuePair,Http Client,HttpParam,HttpPost等。 任何人都可以用适当的格式编写代码吗?

package com.example.nabeel.ask;
public class profile extends AppCompatActivity implements 
View.OnClickListener {

private static final int RESULT_LOAD_IMAGE = 1;
private static final String SERVER_ADDRESS =  
"https://nabeelnazir163.000webhostapp.com/";

ImageView imagetoupload , downloadedimage;
EditText etuploadname, etdownloadname;
Button btn_upload , btn_download;

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

    imagetoupload = (ImageView)findViewById(R.id.imagetoupload);
    etuploadname = (EditText) findViewById(R.id.etuploadname);
    etdownloadname = (EditText)findViewById(R.id.etdownloadname);
    btn_upload = (Button)findViewById(R.id.btn_upload);
    downloadedimage = (ImageView)findViewById(R.id.downloadedImage);

    btn_download = (Button)findViewById(R.id.btn_download);
    imagetoupload.setOnClickListener(this);
    btn_upload.setOnClickListener(this);
}

@Override
 public void onClick(View view) {
    switch (view.getId()){

        case R.id.imagetoupload:
            Intent galleryintent = new Intent(Intent.ACTION_PICK, 
 MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryintent , RESULT_LOAD_IMAGE);
            break;
        case R.id.btn_upload:

            Bitmap Image = ((BitmapDrawable) 
 imagetoupload.getDrawable()).getBitmap();
            new UploadImage(Image , etuploadname.getText().toString());
            break;
        case R.id.btn_download:
            new 
 DownloadImage(etdownloadname.getText().toString()).execute();
            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data 
!= null){
        Uri SelectedImage = data.getData();
        imagetoupload.setImageURI(SelectedImage);
    }
}

private class UploadImage extends AsyncTask<Void , Void , Void>{

    Bitmap image;
    String name;
    public UploadImage(Bitmap image , String name){
        this.image = image;
        this.name = name;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Toast.makeText(getApplicationContext() , "Image Uploaded" , 
 Toast.LENGTH_SHORT).show();
    }

    @Override
    protected Void doInBackground(Void... voids) {

        ByteArrayOutputStream byteArrayOutputStream =  new 
 ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100 , 
 byteArrayOutputStream);

        String encodedImage = 
 Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);

        ArrayList<NameValuePair> dataTosend = new ArrayList<>();
        dataTosend.add(new BasicNameValuePair("image" , encodedImage));
        dataTosend.add(new BasicNameValuePair("name" , name));

        HttpParams httpRequestParams = getHttpRequestParams();

        HttpClient client = new DefaultHttpClient(httpRequestParams);
        HttpPost post = new HttpPost(SERVER_ADDRESS + "savepicture.php");

        try{
            post.setEntity(new UrlEncodedFormEntity(dataTosend));
            client.execute(post);
        }
        catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
 }

 private class DownloadImage extends AsyncTask<Void , Void , Bitmap>{
    String name;
    public DownloadImage(String name){
        this.name = name;
    }
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);

        if(bitmap != null){
            downloadedimage.setImageBitmap(bitmap);
        }
    }

    @Override
    protected Bitmap doInBackground(Void... voids) {
        String url = SERVER_ADDRESS + "pictures/" + name + ".JPG";

        try{
            URLConnection connection = new URL(url).openConnection();
            connection.setConnectTimeout(1000 * 30);
            connection.setReadTimeout(1000 * 30);

            return BitmapFactory.decodeStream((InputStream) 
 connection.getContent(), null , null );
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

private HttpParams getHttpRequestParams(){
    HttpParams httpRequestParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpRequestParams , 1000 *  
30);
    HttpConnectionParams.setSocketBufferSize(httpRequestParams , 1000 * 30);

     return httpRequestParams;
 }
}

0 个答案:

没有答案