简体   繁体   中英

How to call the Image upload web service (ASP.NET ) .asmx file in android using Retrofit

I am trying to upload an image file in the folder from android to asp.net(Web Service- .asmx file) using retrofit.

I have created the web method for that and that web method is working appropriately and I tested on postman also.

But when I tried to upload from android then it displays the "500 Internal Server Error"

Web Method (ASP.NET .asmx file)

[WebMethod]
public void UploadImage_http()
{
    var request = HttpContext.Current.Request;
    var photo = request.Files["photo"];
    photo.SaveAs(HttpContext.Current.Server.MapPath("images/thumbnail/"+photo.FileName));
    JavaScriptSerializer js = new JavaScriptSerializer();
    Feed feed = new Feed();
    feed.err = false;
    feed.err_message = "inserted success";
    Context.Response.Write(js.Serialize(feed));
}

Android Side Code

API Interface

 @Multipart
    @POST("/FeedAPI.asmx/UploadImage_http")
    public void upload(@Part("photo") MultipartBody.Part photo, Callback<Feed> callback);

API Call

    public void btnSelectSave(View view)
        {
            File file = new File(selectedImagePath); 
// selectedImagePath contains the path of the image.
            Toast.makeText(this, file.getName(), Toast.LENGTH_SHORT).show();
            RequestBody photoContent = RequestBody.create(MediaType.parse("multipart/form-data"),file);
            MultipartBody.Part photo = MultipartBody.Part.createFormData("photo",file.getName(),photoContent);
            Log.e("DATA_ photos", ""+photo);

            retrofitService.getService().upload(photo, new Callback<Feed>() {
                @Override
                public void success(Feed feed, Response response) {
                    Toast.makeText(SharePostActivity.this, feed.getErr_message(), Toast.LENGTH_SHORT).show();
                }
                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(SharePostActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }

Below Method use to get the path and value were store in selectedImagePath

public String getRealPathFromURI(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;
}

Error Occurs:

I/System.out: [OkHttp] sendRequest<<
D/Retrofit: <--- HTTP 500 http://192.168.43.25:81/FeedAPI.asmx/UploadImage_http (84ms)
    : HTTP/1.1 500 Internal Server Error
D/Retrofit: Cache-Control: private
    Content-Length: 55
    Content-Type: text/plain; charset=utf-8
    Date: Thu, 12 Sep 2019 14:21:21 GMT
    Server: Microsoft-IIS/10.0
    X-Android-Received-Millis: 1568298080946
    X-Android-Response-Source: NETWORK 500
    X-Android-Selected-Protocol: http/1.1
D/Retrofit: X-Android-Sent-Millis: 1568298080929
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
D/Retrofit: Object reference not set to an instance of an object.
D/Retrofit: <--- END HTTP (55-byte body)

please try this

    [HttpPost, DisableRequestSizeLimit]
    public ActionResult UploadFile(IFormFile file)
    {
       using (var stream = file.OpenReadStream())
       {
       }
    }

邮差

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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