作为小组大学项目的一部分,我们使用PHP休息服务器将数据发送到Oracle服务器。到目前为止,所有get方法都运行正常,但我无法理解如何发布数据,因为它不起作用。
这是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);
});
但我不能为我的生活让它在Android中运行。
这是我用来发布数据的方法:
@FormUrlEncoded
@POST("/room")
Room postRoom(Integer roomId, Double pcm, Integer propID);
来自活动
public void insertARoom(){
RestService.INSTANCE.getService().postRoom(200, 50.0, 103);
}
它所说的错误是它需要在参数1上进行改装注释。 我也尝试使用类型属性的回调,但这仍然给我带来了问题。
理想情况下,第一个参数应为null,因为它是主键,我该怎么做?
由于 奥马
编辑:
Logcat输出,但仍有Internet权限。
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)
其余适配器正在使用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;
}
答案 0 :(得分:1)
为了发送您的数据,您必须使用@Field
注释为每个参数添加“前缀”,如下所示:
@FormUrlEncoded
@POST("/room")
Room postRoom(@Field("roomId") Integer roomId, @Field("pcm") Double pcm, @Field("propID") Integer propID);
答案 1 :(得分:0)
对此迟到的回复道歉。
我解决了。主要是我对Retrofit和Slim如何运作缺乏了解。
这应该是这样的:
@POST("/room")
void postRoom(@Body Room room, Callback<Integer> rc);
我在Android应用程序中所做的只是传递一个类型为Room的参数,在PHP端,它使用JSON解码正文并获取结果。
再次感谢,
奥马