Reputation: 168
I want to create app can send string (json) in a activity to another activity, I check in first activity, I think normal, because systemlog show the json. But, in second activity I can't see json just [ ]. i think second activity not retrieve data, so what can I do?
This is my first Activity :
String data = response.body().string();
System.out.println(data);
Intent kirim = new Intent(this, UploadImage.class);
kirim.putExtra(datakirim, data.toString());
startActivity(kirim);
finish();
And, this Second Activity :
kirim = getIntent();
olahdata = kirim.getStringExtra(MainActivity.datakirim);
System.out.println(olahdata);
This is my log :
01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter I/System.out: [] 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: org.json.JSONException: Index 0 out of range [0..0) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at org.json.JSONArray.get(JSONArray.java:282) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at org.json.JSONArray.getJSONObject(JSONArray.java:510) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at com.example.client18.androidwithcodeigniter.UploadImage.onCreate(UploadImage.java:78) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at android.app.Activity.performCreate(Activity.java:5336) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:135) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at android.os.Looper.loop(Looper.java:136) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5021) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at java.lang.reflect.Method.invoke(Method.java:515) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 01-12 10:58:57.047 17527-17527/com.example.client18.androidwithcodeigniter W/System.err: at dalvik.system.NativeStart.main(Native Method) 01-12 10:58:57.057 17527-17527/com.example.client18.androidwithcodeigniter D/dalvikvm: GC_FOR_ALLOC freed 403K, 14% free 3254K/3744K, paused 15ms, total 15ms 01-12 10:58:57.097 17527-17527/com.example.client18.androidwithcodeigniter I/dalvikvm-heap: Grow heap (frag case) to 4.583MB for 1440012-byte allocation 01-12 10:58:57.127 17527-17535/com.example.client18.androidwithcodeigniter D/dalvikvm: GC_FOR_ALLOC freed 15K, 10% free 4645K/5152K, paused 35ms, total 35ms 01-12 10:58:57.147 2120-2120/com.bluestacks.home D/Home.AppLaunchReceiver: *************************Received app launch broadcast 01-12 10:58:57.157 2120-2120/com.bluestacks.home D/Home.AppLaunchReceiver: APP LAUNCH com.example.client18.androidwithcodeigniter 01-12 10:58:57.177 1742-2982/system_process V/WindowManager: isVisibleLw false for win : Window{4ea71844 u0 com.example.client18.androidwithcodeigniter/com.example.client18.androidwithcodeigniter.MainActivity EXITING} 01-12 10:58:57.187 1871-2943/com.android.inputmethod.latin I/LatinIME:LogUtils: Dictionary info: dictionary = contacts.en_US.dict ; version = ? ; date = ? 01-12 10:58:57.207 1934-18112/com.bluestacks.BstCommandProcessor D/dalvikvm: GC_FOR_ALLOC freed 2292K, 25% free 7623K/10076K, paused 48ms, total 48ms 01-12 10:58:57.227 1934-18112/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: ping 01-12 10:58:57.237 1934-18112/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"result":"ok"} 01-12 10:58:57.237 1742-2982/system_process D/dalvikvm: GC_FOR_ALLOC freed 1880K, 24% free 8356K/10988K, paused 44ms, total 44ms 01-12 10:58:57.247 1934-18115/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: ping 01-12 10:58:57.247 1934-18115/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"result":"ok"} 01-12 10:58:57.327 1934-18116/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: getdefaultlauncher 01-12 10:58:57.327 1934-18119/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: getlaunchactivityname com.example.client18.androidwithcodeigniter 01-12 10:58:57.327 1934-18119/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"value":"androidwithcodeigniter","result":"ok"} 01-12 10:58:57.337 1934-18116/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"defaultLauncher":"com.bluestacks.gamepophome","result":"ok"} 01-12 10:58:57.397 1934-18123/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: ping 01-12 10:58:57.397 1934-18123/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"result":"ok"} 01-12 10:58:57.407 1934-18124/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: getdefaultlauncher 01-12 10:58:57.407 1934-18124/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"defaultLauncher":"com.bluestacks.gamepophome","result":"ok"} 01-12 10:58:57.477 1742-10091/system_process D/InputMethodManagerService: ime_enabled = true is same as last value, no change 01-12 10:58:57.477 1742-10091/system_process D/InputMethodManagerService: ime_enabled = true is same as last value, no change 01-12 10:58:57.517 1742-1742/system_process D/NotificationService: In writeNotificationInfo: pkgName android 01-12 10:58:57.517 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Service: startService called with arg: notification 01-12 10:58:57.517 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Service: New Notification has been observed, informing frontend now 01-12 10:58:57.527 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Application: in sendHttpRequest, request to send to (fqdn): http://10.0.2.2:2861/notification
data: {"content":"Choose input method*bst*English (US) - Android Keyboard (AOSP)","id":"17040512","pkg":"android","tickerText":"android"} 01-12 10:58:57.547 1742-1742/system_process D/NotificationService: In writeNotificationInfo: pkgName android 01-12 10:58:57.557 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Service: startService called with arg: notification 01-12 10:58:57.557 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Service: New Notification has been observed, informing frontend now 01-12 10:58:57.557 1934-1934/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Application: in sendHttpRequest, request to send to (fqdn): http://10.0.2.2:2861/notification
data: {"content":"Choose input method*bst*English (US) - Android Keyboard (AOSP)","id":"17040512","pkg":"android","tickerText":"android"} 01-12 10:58:57.707 1742-1755/system_process V/WindowManager: isVisibleLw false for win : Window{4ea71844 u0 com.example.client18.androidwithcodeigniter/com.example.client18.androidwithcodeigniter.MainActivity EXITING} 01-12 10:58:57.717 1742-1755/system_process I/ActivityManager: Displayed com.example.client18.androidwithcodeigniter/.UploadImage: +776ms 01-12 10:58:57.737 1742-1755/system_process W/WindowManager: Rebuild removed 3 windows but added 2
java.lang.RuntimeException: here
at com.android.server.wm.WindowManagerService.rebuildAppWindowListLocked(WindowManagerService.java:8270)
at com.android.server.wm.WindowManagerService.rebuildAppWindowListLocked(WindowManagerService.java:8206)
at com.android.server.wm.WindowManagerService.handleAnimatingStoppedAndTransitionLocked(WindowManagerService.java:8950)
at com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLockedInner(WindowManagerService.java:9464)
at com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLockedLoop(WindowManagerService.java:8422)
at com.android.server.wm.WindowManagerService.performLayoutAndPlaceSurfacesLocked(WindowManagerService.java:8364)
at com.android.server.wm.WindowManagerService.access$400(WindowManagerService.java:168)
at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:7398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
Upvotes: 0
Views: 276
Reputation:
From your first Activity Code, remove finish(); and then try.
It might help because, calling finish(); ends the Activity.
Upvotes: 0
Reputation: 3632
I think your issue is with your key which you are using in your second activity. Just do one thing use static key value at both places in activity one and activity two rather than the use of MainActivity.datakirim. It will solve your issue.
Issue : Because you are finishing your first activity so your key will not be same whatever you are using at the time of set and get.
If you are not getting still then debug your app and check whatever key you had passed from first activity and what you get in second activity both are same or not.
You can do the same using Bundle in android like:
In ActivityOne do this to pass bundle-
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("String_Stuff_Key", "Your string value");
//key value should be static
//Add the bundle to the intent
intent.putExtras(bundle);
//Fire that second activity
startActivity(intent);
//finish the current activity
finish();
Now in ActivityTwo:
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String stuff = bundle.getString("String_Stuff_Key");
Upvotes: 1
Reputation: 357
Get intent value in 2nd Activity:getIntent().getStringExtra("datakirim");
put intent value in 1ST Activity:kirim.putExtra("datakirim",data);
Upvotes: 1
Reputation: 280
Here you have "finish()" your first activity, and that's why your not able to get value of "MainActivity.datakirim" in second activity.
Solution:
1). Create interface: AppConstants.java
public interface AppConstants {
String EXTRA_DATA_KIRIM = "dataKirim";
}
2). FirstActivity.java
String data = response.body().string();
System.out.println(data);
Intent kirim = new Intent(this, UploadImage.class);
kirim.putExtra(AppConstants.EXTRA_DATA_KIRIM, data.toString());
startActivity(kirim);
finish();
3). UploadImage.java
String olahdata = getIntent().getStringExtra(AppConstants.EXTRA_DATA_KIRIM);
System.out.println(olahdata);
Upvotes: 0