2015-04-17 31 views
0

我有一个小问题,我卡住了。问题是我已经通过一个代码下面的代码。我无法检索通过putExtra传递给Activity的数据?

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

回答

2

意图从哪里来?有getIntent()或意图从方法来像onNewIntent()

而且我认为这是更短的

if(getIntent().hasExtra("classification_id")) { 
    String classId = getIntent().getStringExtra("classification_id"); 
} 

至于将字符串转换网址,你将不堪重负,如果有很多的参数(顺便说一句,我认为这是错误的格式: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(); 
} 
+0

谢谢!问题解决了 ! – user3264924

相关问题