現在、サーバーからjsonを取得するアプリに取り組んでいます。複数のデバイスでアプリをテストしていますが、SIM カードは 1 つしかありません。したがって、デバイスでテストするには、SIM カードをそのデバイスに移動する必要があります。アプリが APN 経由でサーバーに接続できない場合、結果はありません。
私がしたことは、上記のjsonのインスタンスをリソースに保存し、デバッグモードのときにそれを結果として使用することです。このようにして、毎回 SIM カードを切り替えることなく、すべて (接続/要求を除く) をテストできます。
private class RequestTask extends AsyncTask< String, String, String > {
...
@Override
protected void onPostExecute( String pResult ) {
...
if ( retrieveFromRawResource( pResult ) ) {
pResult = CustomUtils.parseRawResource( getActivity().getResources(), R.raw.debugjson );
}
...
}
private boolean retrieveFromRawResource( String pResult ) {
return !isValidResult( pResult ) && CustomUtils.isDebugMode( getActivity() );
}
private boolean isValidResult( String pResult ) {
return ( pResult != null && !pResult.isEmpty() );
}
...
}
public class CustomUtils {
...
public static String parseRawResource( Resources pResources, int pResourceId ) {
StringBuilder builder = new StringBuilder();
String line;
try {
InputStream is = pResources.openRawResource( pResourceId );
BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );
while ( ( line = reader.readLine() ) != null )
{
builder.append( line );
builder.append( "\n" );
}
return builder.toString();
} catch ( Exception e ) {
return null;
}
}
...
public static boolean isDebugMode( Context pContext ) {
return ( ( pContext.getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE ) != 0 );
}
...
}
これは問題なく動作しますが、リリース APK に「未使用」のリソースが存在するという欠点があります。このファイルは非常に大きいため、すべてのリリースから削除することをお勧めします。
毎回手動で削除/追加する必要なく、このようなことは可能ですか? 多分antとProguardの組み合わせを使用していますか?コンパイルする前に生のjsonファイルを一時的に削除して後で置き換えることもできますが、そのリソースへの参照は、呼び出されなくてもコード内に残っています。