2017-10-09 74 views
0

这是我的MainActivity文件。我试图从外部存储中选择一个Image文件并将其上传到服务器,并显示百分比进度。 我想添加ProgressDialog它,我试图添加它,但它投掷运行时错误如何在okhttp3中添加进度对话框out of listener in android

package com.example.panchayat.myapplication; 

import android.support.v7.app.AppCompatActivity; 
import android.support.v4.app.ActivityCompat; 
import android.os.Bundle; 
import android.Manifest; 
import android.util.Log; 
import android.widget.Toast; 
import android.content.pm.PackageManager; 
import android.content.Intent; 
import android.net.Uri; 
import java.io.File; 
import java.io.IOException; 
import android.database.Cursor; 
import android.provider.MediaStore; 
import okhttp3.Request; 
import okhttp3.Response; 
import okhttp3.OkHttpClient; 
import okhttp3.Callback; 
import okhttp3.Call; 
import okhttp3.MediaType; 
import okhttp3.MultipartBody; 
import okhttp3.RequestBody; 
import android.os.Handler; 
import android.app.ProgressDialog; 
import com.example.panchayat.myapplication.CountingFileRequestBody; 
import com.example.panchayat.myapplication.CountingFileRequestBody.ProgressListener; 
import com.example.panchayat.myapplication.ProgressRequestBody.Listener; 
import com.example.panchayat.myapplication.ProgressRequestBody; 

public class MainActivity extends AppCompatActivity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(galleryIntent, 1); 
     ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1); 
    } 
    @Override 
    public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case 1: { 

       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        // permission was granted, yay! Do the 
        // contacts-related task you need to do. 
       } else { 
        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
        Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show(); 
        //finishAffinity(); 
        //System.exit(0); 
       } 
       return; 
      } 

      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 
    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == this.RESULT_CANCELED) { 
      return; 
     } 
     if (requestCode == 1) { 
      if (data != null) { 
       Uri uri = data.getData(); 

       File myFile = new File(getRealPathFromURI(uri)); 
       final OkHttpClient client = new OkHttpClient(); 
       final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png"); 
       RequestBody requestBody = new MultipartBody.Builder() 
         .setType(MultipartBody.FORM) 
         .addFormDataPart("title", "Square Logo") 
         .addFormDataPart("image", "logo-square.png", RequestBody.create(MEDIA_TYPE_PNG, myFile)) 
         .build(); 

       //CountingFileRequestBody ok = new CountingFileRequestBody(requestBody, "image/jpg",progressListener); 
       ProgressRequestBody ioo = new ProgressRequestBody(requestBody,progressListener); 
       Toast.makeText(MainActivity.this, myFile.getAbsolutePath(), Toast.LENGTH_SHORT).show(); 

       Request request = new Request.Builder() 
         .url("http://bmi.ir/multi.php") 
         .post(ioo) 
         .build(); 

       client.newCall(request).enqueue(new Callback() { 
          @Override 
          public void onFailure(Call call, IOException e) { 

           Log.d("ok",e.getMessage()); 
          } 
          @Override 
          public void onResponse(Call call, Response response) throws IOException { 
           Log.d("ok",response.body().string()); 
           //Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show(); 
          } 
         } 
       ); 
      } 
     } 
    } 
    final Listener progressListener = new Listener() { 
     @Override public void onProgress(int bytesRead) { 
      //Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show(); 
      final String myString = Long.toString(bytesRead); 
      final int per = bytesRead; 
      Log.d("ok", myString); 
     } 
    }; 

    private String getRealPathFromURI(Uri contentURI) 
    { 
     String result = null; 

     Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); 

     if (cursor == null) 
     { // Source is Dropbox or other similar local file path 
      result = contentURI.getPath(); 
     } 
     else 
     { 
      if(cursor.moveToFirst()) 
      { 
       int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
       result = cursor.getString(idx); 
      } 
      cursor.close(); 
     } 
     return result; 
    } 
} 

如何从听众处获得进度%并更新ProgressDialog

+0

你能发表你的错误吗? –

回答

0
@Override 
public void onProgress(int bytesRead) { 
    //Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show(); 
    double percentage = 100 * bytesRead/fileLength; 
    Log.d(TAG,"Progress percentage: " + percentage); 
} 
+0

我需要添加进度对话框,onProgress已经获得百分比,不需要添加''double percentage = 100 * bytesRead/fileLength; '' – panchayat