改造DELETE Android

时间:2015-03-30 07:46:36

标签: android retrofit

我真的很擅长改造,我正在试图找出如何进行删除

这就是我所拥有的:

interface DeleteNote {
        @DELETE("/staff/users/note")
        Response deleteResponse(
            @Query("note_id") String note_id
        );
    }

这是我使用它的地方

RestAdapter restAdapter = new RestAdapter.Builder()
                                                .setEndpoint(Utils.getApiBaseUrl())
                                                .build();

 // Create an instance of our GitHub API interface.
    DeleteNote dn = restAdapter.create(DeleteNote.class);
    Response response = dn.deleteResponse(n.note_id);

这是我得到的错误:

Caused by: android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
            at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
            at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
            at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
            at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
            at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
            at com.wisely.loyaltyforstaff.activities.$Proxy0.deleteResponse(Native Method)
            at com.wisely.loyaltyforstaff.activities.ProfileActivity$NotesAsync$1.onDismiss(ProfileActivity.java:664)
            at com.wisely.loyaltyforstaff.utils.SwipeDismissTouchListener$2.onAnimationEnd(SwipeDismissTouchListener.java:257)
            at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1056)
            at android.animation.ValueAnimator.access$400(ValueAnimator.java:50)
            at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:644)
            at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:660)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:543)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
编辑:我没有包括我在改装页面上简要提到的okhttp - 我会尝试并回来。

1 个答案:

答案 0 :(得分:2)

您必须使用Retrofit CallBack或将您的方法包装在AsyncTask中。请检查Synchronous Vs. Asynchronous Vs. Observable

来自their website

会话。

回调的代码应该是这样的,

interface DeleteNote {
    @DELETE("/staff/users/note") 
    Response deleteResponse(
        @Query("note_id") String note_id, 
        Callback<YourObjectHere> myCallBack
    );
  }

或者,如果您根本不想要CallBack,只需将现有代码包装在doInBackground的{​​{1}}方法中,然后执行该任务。

会是这样的。

AsyncTask