1
AVDでは、Webサービスhttp://10.0.2.2:8080/details
をテストしたところ、正常に動作しました。しかし、私のアンドロイドアプリケーションでは、getForObject
メソッドはnull
をdoInBackground
に返します。ここに何が間違っているのか分かりません。私を助けてください。以下は私のコードです:アンドロイドのRESTコールでdoInBackgroundがnullを返すのはなぜですか?
たManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.myapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.user.myapp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//Added by me
packagingOptions {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/notice.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
compile 'com.android.support:design:25.3.1'
//****** Mes dependencies for Android RestTemplate and Spring interaction
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.2'
//*****END
testCompile 'junit:junit:4.12'
}
私のデータクラス
@JsonIgnoreProperties(ignoreUnknown = true)
public class Moyennes {
private String id,name,age;
/*Getters and setters*/
//Constructor
public Students(){
}
}
**マイアクティビティクラス**
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
new HttpRequestTask().execute();
}
private class HttpRequestTask extends AsyncTask<Void, Void, Students> {
@Override
protected Students doInBackground(Void... params) {
// To allow Debugging
if(android.os.Debug.isDebuggerConnected())
android.os.Debug.waitForDebugger();
try {
final String url = "http://10.0.2.2:8080/details";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Students students = restTemplate.getForObject(url, Students.class);
return students ;
} catch (Exception e) {
// Log.e("MainActivity", e.getMessage(), e);
Message.message(context,e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Students student) {
String id = student.getId();
String name = student.getName();
String age = student.getAge();
// some other work
}
}
あなたは 'Log'キャッチ例外は理由を知っている必要があります。
は以下doInBackground
に加えられた変更があり、それを解決するには。 – Sufian@Sufian ok ..私のコンピュータは遅い(AVDに感謝しています)...可能な 'Log'ですぐに戻る...実際には' Log'はあなたについて話していますか?今のところ、例外は検出されないためです。 'doInBackgroung'はうまく戻りますが、' students = null'でクロム(AVD)のWebサービスは空のものを返しません....私はどこかに何か間違ったことをしたので、 'getForObject'が失敗したようです... –
それは投げますtry catchブロック内の例外とそれをログに記録していません –