我无法使用 putExtra 检索传递给 Activity 的数据?
I cannot retrieve the data passed to a Activity with putExtra?
我遇到了一个小问题,卡住了。问题是我用下面的代码传递了一个 id。
Intent i = new Intent(First.this,Second.class);
i.putExtra("classification_id","3");
但是,当我尝试使用下面的代码获取参数 3 时,在调试模式下检查它时得到的结果是 -1。
if(intent.getExtras().getString("classification_id")!=null){
classId = intent.getExtras().getString("classification_id");
}else{
classId = "1";
}
其实我想用这个参数设置成一个url得到json数据得到json数据。但这是正确的方法吗?还是将 String int 设置为 url 是一种不好的做法?前任。 "www.test.test/myid?="+classId
意图来自哪里? getIntent()
或 Intent 来自 onNewIntent()
等方法
我也觉得这个更短
if(getIntent().hasExtra("classification_id")) {
String classId = getIntent().getStringExtra("classification_id");
}
至于在url中插入String,如果参数很多,你会不知所措(顺便说一句,我认为这是一个错误的格式:www.example.test/myid?=classId也许这就是你想要的 www.example.com/test?myid=classId )。所以我们可以做
private static final String URL="https://www.example.com";
private static final String PATH = "test";
private static final String PARAM_MYID = "myid";
public static String buildMyUrl(String id){
Uri.Builder b = Uri.parse(URL).buildUpon();
b.path(PATH);
b.appendQueryParameter(PARAM_MYID, id);
b.build();
return b.toString();
}
我遇到了一个小问题,卡住了。问题是我用下面的代码传递了一个 id。
Intent i = new Intent(First.this,Second.class);
i.putExtra("classification_id","3");
但是,当我尝试使用下面的代码获取参数 3 时,在调试模式下检查它时得到的结果是 -1。
if(intent.getExtras().getString("classification_id")!=null){
classId = intent.getExtras().getString("classification_id");
}else{
classId = "1";
}
其实我想用这个参数设置成一个url得到json数据得到json数据。但这是正确的方法吗?还是将 String int 设置为 url 是一种不好的做法?前任。 "www.test.test/myid?="+classId
意图来自哪里? getIntent()
或 Intent 来自 onNewIntent()
我也觉得这个更短
if(getIntent().hasExtra("classification_id")) {
String classId = getIntent().getStringExtra("classification_id");
}
至于在url中插入String,如果参数很多,你会不知所措(顺便说一句,我认为这是一个错误的格式:www.example.test/myid?=classId也许这就是你想要的 www.example.com/test?myid=classId )。所以我们可以做
private static final String URL="https://www.example.com";
private static final String PATH = "test";
private static final String PARAM_MYID = "myid";
public static String buildMyUrl(String id){
Uri.Builder b = Uri.parse(URL).buildUpon();
b.path(PATH);
b.appendQueryParameter(PARAM_MYID, id);
b.build();
return b.toString();
}