gpt4 book ai didi

php - 在 Android 中将 Retrofit 发布到 Rest 服务器

转载 作者:行者123 更新时间:2023-11-30 02:59:33 28 4
gpt4 key购买 nike

作为大学小组项目的一部分,我们使用 PHP rest 服务器将数据发送到 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);

从 Activity 中

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

它说的错误是它需要对参数 1 进行改造注释。我也尝试过使用 Property 类型的回调,但这仍然给我带来了问题。

理想情况下,第一个参数应该为 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;
}

最佳答案

为了发送您的数据,您必须使用 @Field 注释为每个参数“添加前缀”,如下所示:

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

关于php - 在 Android 中将 Retrofit 发布到 Rest 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22758382/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com