Question

As part of a group university project, we're using a PHP rest server to send data to an Oracle server. So far, all of the get methods work fine, but I'm having trouble understanding how to post data, as it's not working.

This is the PHP

    $app->post('/room', function () use ($app) {
    $request = $app->getInstance()->request();
    $room = json_decode($request->getBody(), true);
    $sql = 'INSERT INTO rooms VALUES(null, :room_pcm, :property_id)';
    $query = oci_parse(getConnection(), $sql);
    $room_pcm = $room['ROOM_PCM'];
    $property_id = $room['PROPERTY_ID'];
    oci_bind_by_name($query, ':room_pcm', $room_pcm);
    oci_bind_by_name($query, ':property_id', $property_id);
    oci_execute($query);
});

But I can't for the life of me get this to work in Android.

This is the method I'm using to post data:

    @FormUrlEncoded
@POST("/room")
Room postRoom(Integer roomId, Double pcm, Integer propID);

And from the activity

    public void insertARoom(){
    RestService.INSTANCE.getService().postRoom(200, 50.0, 103);
}

The error it is saying is it needs a retrofit annotation on parameter 1. I've also tried using a callback of type Property but this still gave me problems.

Ideally, the first parameter should be null as it's a primary key, how could I do this?

Thanks Omar

EDIT:

Logcat output, and yet the Internet permission is there.

03-31 06:15:37.230: E/AndroidRuntime(1913): FATAL EXCEPTION: main
03-31 06:15:37.230: E/AndroidRuntime(1913): Process: prcsc.android, PID: 1913
03-31 06:15:37.230: E/AndroidRuntime(1913): retrofit.RetrofitError: android.os.NetworkOnMainThreadException
03-31 06:15:37.230: E/AndroidRuntime(1913):     at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:425)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:282)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at $Proxy0.postRoom(Native Method)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at prcsc.android.fragment.PropertyDetailFragment$3.onClick(PropertyDetailFragment.java:170)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at android.view.View.performClick(View.java:4438)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at android.view.View$PerformClick.run(View.java:18422)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at android.os.Handler.handleCallback(Handler.java:733)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at android.os.Looper.loop(Looper.java:136)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at java.lang.reflect.Method.invoke(Method.java:515)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at dalvik.system.NativeStart.main(Native Method)
03-31 06:15:37.230: E/AndroidRuntime(1913): Caused by: android.os.NetworkOnMainThreadException
03-31 06:15:37.230: E/AndroidRuntime(1913):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.okhttp.Connection.isReadable(Connection.java:214)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:89)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:48)
03-31 06:15:37.230: E/AndroidRuntime(1913):     at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:357)

The rest adapter is using GSON

    public Rest getService() {
    if (RestService.service == null) {
        Gson gson = new GsonBuilder().registerTypeAdapter(Image.class,
                new ImageDeserialiser()).registerTypeAdapter(CountInteger.class, new CountDeserialiser()).create();
        RestService.service = new RestAdapter.Builder()
                .setEndpoint(RestService.URL)
                .setConverter(new GsonConverter(gson)).build()
                .create(Rest.class);
    }
    return RestService.service;
}
Was it helpful?

Solution 2

apologies for the late reply to this.

I solved it. It was mainly my lack of understanding of how Retrofit and Slim worked.

This is how it should have been:

    @POST("/room")
    void postRoom(@Body Room room, Callback<Integer> rc);

All I had to do from the Android app was pass it a parameter of type Room, and on the PHP side it used JSON to decode the body and fetch the result.

Many thanks again,

Omar

OTHER TIPS

In order to send your data, you have to "prefix" each parameter with a @Field annotation like this:

@FormUrlEncoded
@POST("/room")
Room postRoom(@Field("roomId") Integer roomId, @Field("pcm") Double pcm, @Field("propID") Integer propID);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top