Retrofit-IllegalArgumentException: unexpected url

末鹿安然 提交于 2019-12-10 12:38:39

问题


I want to upload a video file (chosen from gallery) to a server using Retrofit. But it doesn't work and throws the exception "java.lang.IllegalArgumentException: unexpected url: 192.168.1.7". My code is presented below.
PostFile.java:

public final class PostFile {
    public static final MediaType MEDIA_TYPE_MARKDOWN
      = MediaType.parse("vide/mp4");

    private final OkHttpClient client = new OkHttpClient();

    public void run(String path) throws Exception {
        File file = new File(path);

        Request request = new Request.Builder()
            .url("192.168.1.7/")
            .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
            .build();

        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        System.out.println(response.body().string());
    }
}

PostFile:

public class MainActivity extends Activity {
    private static int RESULT_LOAD_IMG = 1;
    String decodableString;

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

    public void loadImagefromGallery(View view) {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedVideo = data.getData();

                String[] filePathColumn = { MediaStore.Video.Media.DATA };
                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedVideo,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                decodableString = cursor.getString(columnIndex);
                cursor.close();
                new PostFile().run(decodableString);
                Log.i("mohsen","done");
            } else {
                Toast.makeText(this, "You haven't picked any video",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }
    }
}

Wampserver is running Apache Server 2.4.4 on my computer. Please note that I have no idea whether this code is sound or not, and I'm just trying to make it work almost blindly.


回答1:


Try change

    .url("192.168.1.7/")

to

    .url("http://192.168.1.7")



回答2:


modify your url to http://192.168.1.7. It should works perfectly.



来源:https://stackoverflow.com/questions/33675010/retrofit-illegalargumentexception-unexpected-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!