質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

0回答

787閲覧

Android(java)でjson取得を行いたいが、ビルドは通るのにアプリが強制終了してしまう。

manaki

総合スコア1

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2021/12/03 07:12

閲覧ありがとうございます。
Android開発でjsonの情報を取得して画面に表示したいのですが、ビルドはエラーなく通るのにAndroid側でアプリ起動すると強制終了する症状にかれこれ3日ほど悩んでいます。

原因に心当たりある方がいれば教えて頂けたら幸いです。

以下が、コードになります。

MainActivity

1package com.example.myapplication; 2 3import androidx.appcompat.app.AppCompatActivity; 4import android.os.Handler; 5import android.os.Looper; 6import android.os.Bundle; 7import android.util.Log; 8import android.widget.TextView; 9 10 11import org.jetbrains.annotations.NotNull; 12import org.json.JSONObject; 13 14import java.io.IOException; 15 16import okhttp3.Call; 17import okhttp3.Callback; 18import okhttp3.OkHttpClient; 19import okhttp3.Request; 20import okhttp3.Response; 21 22 23 24public class MainActivity extends AppCompatActivity { 25 TextView txt01; 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 31 txt01 = findViewById(R.id.txt1); 32 33 //httpリクエスト 34 try{ 35 //okhttpを利用するカスタム関数(下記) 36 httpRequest("http://denisusama.php.xdomain.jp/"); 37 }catch(Exception e){ 38 Log.e("Hoge",e.getMessage()); 39 } 40 } 41 42 void httpRequest(String url) throws IOException{ 43 44 //OkHttpClinet生成 45 OkHttpClient client = new OkHttpClient(); 46 47 //request生成 48 Request request = new Request.Builder() 49 .url(url) 50 .build(); 51 52 //非同期リクエスト 53 client.newCall(request) 54 .enqueue(new Callback() { 55 56 //エラーのとき 57 @Override 58 public void onFailure(@NotNull Call call, @NotNull IOException e) { 59 Log.e("Hoge",e.getMessage()); 60 } 61 62 //正常のとき 63 @Override 64 public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { 65 66 //response取り出し 67 final String jsonStr = response.body().string(); 68 Log.d("Hoge","jsonStr=" + jsonStr); 69 70 //JSON処理 71 try{ 72 //jsonパース 73 JSONObject json = new JSONObject(jsonStr); 74 final String status = json.getString("foodname"); 75 76 //親スレッドUI更新 77 Handler mainHandler = new Handler(Looper.getMainLooper()); 78 mainHandler.post(new Runnable() { 79 @Override 80 public void run() { 81 txt01.setText(status); 82 } 83 }); 84 85 86 }catch(Exception e){ 87 Log.e("Hoge",e.getMessage()); 88 } 89 90 } 91 }); 92 } 93}

activitymain

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:layout_weight="1" 6 android:orientation="horizontal" 7 android:paddingLeft="16dp" 8 android:paddingRight="16dp"> 9 10 <LinearLayout 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:layout_weight="1" 14 android:orientation="vertical"> 15 16 <EditText 17 android:id="@+id/foodName" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:hint="@string/foodName" 21 android:inputType="text" /> 22 23 <EditText 24 android:id="@+id/lipids" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:hint="@string/lipids" 28 android:inputType="numberDecimal" /> 29 30 <EditText 31 android:id="@+id/protein" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:hint="@string/protein" 35 android:inputType="numberDecimal" /> 36 37 <EditText 38 android:id="@+id/sugar" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:hint="@string/sugar" 42 android:inputType="numberDecimal" /> 43 44 <Button 45 android:id="@+id/button" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:text="@string/register" /> 49 </LinearLayout> 50 51 <TextView 52 android:id="@+id/txt1" 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content"/> 55 56 <ListView 57 android:id="@+id/foodList" 58 android:layout_width="53dp" 59 android:layout_height="213dp" /> 60</LinearLayout>

androidmanifest

1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.myapplication"> 4 <uses-permission android:name="android.permission.INTERNET" /> 5 <application 6 android:networkSecurityConfig="@xml/network_security_config" 7 android:allowBackup="true" 8 android:icon="@mipmap/ic_launcher" 9 android:label="@string/app_name" 10 android:roundIcon="@mipmap/ic_launcher_round" 11 android:supportsRtl="true" 12 android:theme="@style/Theme.MyApplication"> 13 <activity 14 android:name=".MainActivity" 15 android:exported="true"> 16 <intent-filter> 17 <action android:name="android.intent.action.MAIN" /> 18 19 <category android:name="android.intent.category.LAUNCHER" /> 20 </intent-filter> 21 </activity> 22 </application> 23 24</manifest>

buidgradle

1plugins { 2 id 'com.android.application' 3} 4 5android { 6 compileSdk 31 7 8 defaultConfig { 9 applicationId "com.example.myapplication" 10 minSdk 21 11 targetSdk 31 12 versionCode 1 13 versionName "1.0" 14 15 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 } 17 18 buildTypes { 19 release { 20 minifyEnabled false 21 proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 } 23 } 24 compileOptions { 25 sourceCompatibility JavaVersion.VERSION_1_8 26 targetCompatibility JavaVersion.VERSION_1_8 27 } 28} 29 30dependencies { 31 implementation 'androidx.appcompat:appcompat:1.2.0' 32 implementation 'com.google.android.material:material:1.3.0' 33 implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 34 testImplementation 'junit:junit:4.+' 35 androidTestImplementation 'androidx.test.ext:junit:1.1.2' 36 androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 37 implementation 'com.squareup.okhttp3:okhttp:4.0.0-alpha02' 38}

networksecurityconfig

1<?xml version="1.0" encoding="utf-8"?> 2<network-security-config> 3 <domain-config cleartextTrafficPermitted="true"> 4 <domain includeSubdomains="true">denisusama.php.xdomain.jp</domain> 5 </domain-config> 6</network-security-config>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問