AndroidからAPIへJSONデータをPOSTしたいです。
通信はできているのですが、
AndroidからAPIへ値を渡すことができません。
①JavaからAPIへJSONデータを渡す
②APIで受け取ったJSONデータで検索をかける
③検索結果をAPIからJava側へ送る。
という流れを実装しようと考えています。
①でつまづいてしまいました。。。
Javaの方はOKHTTPを使用しています。
Android
Java
1/ // POST 2 private void postTest() { 3 MediaType MIMEType= MediaType.parse("application/json; charset=utf-8"); 4 RequestBody body = RequestBody.create(MIMEType, "地下一階"); 5 6 7 Request request = new Request.Builder() 8 .url("https://town-sagae.c9users.io/api/v1/images/search") 9 .post(body) 10 .build(); 11 12 OkHttpClient client = new OkHttpClient(); 13 client.newCall(request).enqueue(new Callback() { 14 @Override 15 public void onFailure(Call call, IOException e) { 16 failMessage(); 17 e.printStackTrace(); 18 } 19 20 @Override 21 public void onResponse(Call call, Response response) throws IOException { 22 res = response.body().string(); 23 System.out.println("res"); 24 System.out.println(res); ①指定したURLのHTML要素が返ってきます。 25 System.out.println("res"); 26 runOnUiThread(new Runnable() { 27 public void run() { 28 textViewRes.setText(res); 29 textViewDes.setText("No Data"); 30 } 31 }); 32 } 33 }); 34 35 } 36
API
Ruby
1module Api 2 module V1 3 class ImagesController < ApplicationController 4 skip_before_filter :verify_authenticity_token 5 6 def index 7 @images = Image.all 8 render json: @images 9 end 10 11 def search ★ここです。 12 json_request = JSON.parse(request.body.read) 13 @images = Image.where(location: json_request) 14 @images.destroy_all 15 end 16 17 end 18 end 19end
エラーは出ていないと思うのですが、ログはこのようになっています。
Java側です。
06-16 12:12:54.135 9456-9456/? I/art: Not late-enabling -Xcheck:jni (already on) 06-16 12:12:54.135 9456-9456/? W/art: Unexpected CPU variant for X86 using defaults: x86 06-16 12:12:54.564 9456-9456/station.around.aroundsatation W/System: ClassLoader referenced unknown path: /data/app/station.around.aroundsatation-1/lib/x86 06-16 12:12:54.574 9456-9456/station.around.aroundsatation I/InstantRun: starting instant run server: is main process 06-16 12:12:54.990 9456-9463/station.around.aroundsatation W/art: Suspending all threads took: 14.908ms 06-16 12:12:55.113 9456-9456/station.around.aroundsatation W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 06-16 12:12:55.465 9456-9456/station.around.aroundsatation D/debug: locationStart() 06-16 12:12:55.467 9456-9456/station.around.aroundsatation D/debug: gpsEnabled 06-16 12:12:55.596 9456-9495/station.around.aroundsatation I/OpenGLRenderer: Initialized EGL, version 1.4 06-16 12:12:55.596 9456-9495/station.around.aroundsatation D/OpenGLRenderer: Swap behavior 1 06-16 12:12:55.596 9456-9495/station.around.aroundsatation W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 06-16 12:12:55.596 9456-9495/station.around.aroundsatation D/OpenGLRenderer: Swap behavior 0 06-16 12:12:55.702 9456-9456/station.around.aroundsatation W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 06-16 12:12:56.907 9456-9456/station.around.aroundsatation D/NetworkSecurityConfig: No Network Security Config specified, using platform default
ご経験ある方、ご助言お願いします。
追加
Java
1 RequestBody body = RequestBody.create(MIMEType, "{\"gps\":\"地下一階\"}");
Ruby
1 def search 2 json_request = JSON.parse(request.body.read) 3 @images = Image.where(location: json_request.gps)★変更★ 4 @images.destroy_all 5 render json: @images 6 end
ログ
06-16 13:55:23.119 346-346/? I/art: Not late-enabling -Xcheck:jni (already on) 06-16 13:55:23.119 346-346/? W/art: Unexpected CPU variant for X86 using defaults: x86 06-16 13:55:23.386 346-346/station.around.aroundsatation W/System: ClassLoader referenced unknown path: /data/app/station.around.aroundsatation-2/lib/x86 06-16 13:55:23.411 346-346/station.around.aroundsatation I/InstantRun: starting instant run server: is main process 06-16 13:55:23.909 346-346/station.around.aroundsatation W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 06-16 13:55:24.219 346-353/station.around.aroundsatation W/art: Suspending all threads took: 17.584ms 06-16 13:55:24.293 346-346/station.around.aroundsatation D/debug: locationStart() 06-16 13:55:24.294 346-346/station.around.aroundsatation D/debug: gpsEnabled 06-16 13:55:24.499 346-391/station.around.aroundsatation I/OpenGLRenderer: Initialized EGL, version 1.4 06-16 13:55:24.499 346-391/station.around.aroundsatation D/OpenGLRenderer: Swap behavior 1 06-16 13:55:24.500 346-391/station.around.aroundsatation W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 06-16 13:55:24.500 346-391/station.around.aroundsatation D/OpenGLRenderer: Swap behavior 0 06-16 13:55:24.559 346-346/station.around.aroundsatation W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 06-16 13:55:25.727 346-346/station.around.aroundsatation D/NetworkSecurityConfig: No Network Security Config specified, using platform default
API
Ruby
1module Api 2 module V1 3 class ImagesController < ApplicationController 4 skip_before_filter :verify_authenticity_token 5 6 def index 7 @images = Image.all 8 render json: @images 9 end 10 11 def search 12 @images = Image.all 13 @images.destroy_all 14 render json: @images 15 end 16 17 end 18 end 19end
System.out.println("request"); Request request = new Request.Builder() .url("http://candii.tk/api/v1/images/search") // HTTPアクセス POST送信 テスト確認用ページ .post(requestBody) .build(); System.out.println("request");
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。