Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
1- I am building an application front/back end using Visual Studio 2022 as the back end for Image upload file and it is tested using Postman it uploads the image to the server Successfully

Postman test Image

2- I am trying to call the API from android studio and I am using the following code


Java
void DoActualRequest(File f)
{
    String Excep_tion;
    String url="http://192.168.1.6:46829/api/ImageUpload";
    Toast.makeText(this, "OKHTTP3_VJ_Files , Calles actual Reuquest"+url,Toast.LENGTH_SHORT).show(); //

     new  Thread()
     {
        @Override
         public void run()
           {
               final int flag;
                try
                {
                    OkHttpClient client=new OkHttpClient();

                    RequestBody  requestBody=new MultipartBody.Builder()
                            .setType(MultipartBody.FORM)
                            .addFormDataPart("files",f.getName(),RequestBody.create(MediaType.parse("image/jpeg"),f) )
                            .build();

                    //  Toast.makeText(this, "OKHTTP3_VJ_Files , Request body generated", Toast.LENGTH_SHORT).show(); //

                    okhttp3.Request request=new  okhttp3.Request.Builder()
                            .url(url)
                            .post(requestBody)
                            .build();

                    try {

                        //Network on Main Thread Exception happened beacuse of long communication with server
                        //without making this commnication in background
                        //https://stackoverflow.com/questions/6343166/how-can-i-fix-android-os-networkonmainthreadexception
                        //to solve this problem we used the thread technique

                        okhttp3.Response response = client.newCall(request).execute();
                        //  Toast.makeText(this, "response"+response.body().toString(), Toast.LENGTH_SHORT).show(); //
                        //---------------------------when there is response send the results to the main thread------------
                        if(response.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "successful response", Toast.LENGTH_SHORT).show();
                            flag=1;
                        }
                        else {
                            Toast.makeText(MainActivity.this, "" + response.body(), Toast.LENGTH_SHORT).show();
                            flag=0;
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // Post the result to the main thread
                               if(flag==1)
                                   Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                               else
                                   Toast.makeText(MainActivity.this, "NOT OK", Toast.LENGTH_SHORT).show();
                            }
                        });
                        //----------------------------------------



                    }catch(Exception ex)
                    { //ex.getCause().toString();
                        //Log.d("OKHTTP3_VJ_Files","Exception Occured");
                        //Toast.makeText(this,ex.getMessage().toString() , Toast.LENGTH_SHORT).show();
                        Toast.makeText(MainActivity.this, ex.getMessage().toString(), Toast.LENGTH_SHORT).show();
                        ex.printStackTrace();
                    }


                }catch(Exception ex)
                {
                    Toast.makeText(MainActivity.this, ex.getCause().toString(), Toast.LENGTH_SHORT).show();
                    ex.printStackTrace();

                }



           }//end run function



     }.start();


}//Do Actual Request



3-when I am trying to use the above function I got error : 400 Bad request ? even it is tested in postman ,i tried the to test other Browser but the problem persists , Any explanation for this error 5- Also I directly tested the link in the Browser i got the following Error when using the IP address 192.168.1.6


Error 400

4-and when using the URL : http://localhost:46829/api/ImageUpload i got the folowing Error 405


Error 405

thanks in advance


What I have tried:

actually i tested the Back end with Postman and it is working
Posted
Comments
David Crow 9-Jul-22 18:55pm    
"3-when I am trying to use the above function I got error : 400 Bad request ?"

Which statement is producing this error?


"i tried the to test other Browser but the problem persists , Any explanation for this error 5- Also I directly tested the link in the Browser i got the following Error when using the IP address 192.168.1.6"

If the URL is not working in your browser, then it has nothing (yet) to do with Android.
Member 13639193 10-Jul-22 3:10am    
when i execute okhttp3.Response response = client.newCall(request).execute();
CoderzF1 16-Sep-22 0:55am    
sounds like a server issue, not exactly an android app issue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900