哑剧多部分流的意外结束

时间:2018-07-02 20:48:57

标签: java c# android asp.net-web-api2

我正在尝试通过asp.net Web API使用android应用程序上载图像,当我使用邮递员上载图像时,它可以正常工作,但是当我使用android应用程序时,它将引发此异常

 value unexpected end of mime multipart stream multipart stream.MIME multipart messege is not complete. of type java.lang.string cannot be converted to jasnobject

这是我的android代码

public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private Button btnChoose, btnUpload;
private ProgressBar progressBar;

public static String BASE_URL = "http://192.168.43.145/Election/api/upload";
static final int PICK_IMAGE_REQUEST = 1;
String filePath;

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

    imageView = (ImageView) findViewById(R.id.imageView);
    btnChoose = (Button) findViewById(R.id.button_choose);
    btnUpload = (Button) findViewById(R.id.button_upload);

    btnChoose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            imageBrowse();
        }
    });

    btnUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (filePath != null) {
                imageUpload(filePath);
            } else {
                Toast.makeText(getApplicationContext(), "Image not selected!", Toast.LENGTH_LONG).show();
            }
        }
    });
}

private void imageBrowse() {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {

        if(requestCode == PICK_IMAGE_REQUEST){
            Uri picUri = data.getData();

            filePath = getPath(picUri).toString();

            Log.d("picUri", picUri.toString());
            Log.d("filePath", filePath);

            imageView.setImageURI(picUri);

        }

    }

}

private void imageUpload(final String imagePath) {

    SimpleMultiPartRequest smr = new SimpleMultiPartRequest(Request.Method.POST, BASE_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d("Response", response);
                    try {
                        JSONObject jObj = new JSONObject(response);
                        String message = jObj.getString("message");

                        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();

                    } catch (JSONException e) {
                        // JSON error
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

    smr.addFile("image", imagePath.toString());
    MyApplication.getInstance().addToRequestQueue(smr);

}

private String getPath(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String result = cursor.getString(column_index);
    cursor.close();
    return result;
}

  }

这也是我的api代码

public async Task<string> Post()
    {

        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);
        try
        {
            await Request.Content.ReadAsMultipartAsync(provider);



            var httpRequest = HttpContext.Current.Request;

            if (httpRequest.Files.Count > 0)
            {
                foreach(string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];

                    var filename = postedFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();

                    var filePath = HttpContext.Current.Server.MapPath("~/Uploads/"+ filename);

                    postedFile.SaveAs(filePath);

                    return "/uploads/" + filename;
                }
            }
            else
            {
                return "file was not uploaded";
            }
        }

        catch (Exception exception)
        {
            return exception.Message;
        }
        return "hi";
    }
}
 }

我认为我的asp.met网络api代码没有问题,因为它可以通过PostMan进行工作

任何帮助将不胜感激

0 个答案:

没有答案