我已经开发了一个Android应用程序。
这里我必须将字符串值传递给 url。请告诉我如何将字符串值传递给 url。给我解决方案。
String mTitle;
String URL = "http://api1.sfsfsfffsf.com/dev/categories/?fields=&categoryid="+mTitle+"&access_token=bfb787a6e1";
static String KEY_CATEGORY = "category";
// public static final String URL_Address=null;
static final String KEY_TITLE = "categoryName";
static final String KEY_NAME ="categoryId";
static final String KEY_SUBCATE = "categoryId";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subcate);
Bundle b = getIntent().getExtras();
mTitle = b.getString(KEY_SUBCATE);
TextView grandtotal = (TextView) findViewById(R.id.subcate);
grandtotal.setText("Welcome ," + mTitle );
这是我的网址: [http://api1.sfsfsfsff.com/dev/categories/?fields=&categoryid=10&access_token=bfb787a6e1112][1]
我直接提到categoryid=10表示已显示所有产品。
但我必须更改此网址,例如: [http://api1.sfsfsfsfsf.com/dev/categories/?fields=&categoryid="+mTitle+"&access_token=bfb787a6e1112][2]
我已经更改了我的网址,例如 categoryid="+mTitle+" 表示未显示所有产品。
mTitle 值是从我之前的 Activity 中获取的。
我的代码出了什么问题。
编辑:
我的 mTitle 值是 10。我必须编写类似的代码
grandtotal.setText("欢迎光临,"+ mTitle);
成功显示mTitle值。
grandtotal.setText("欢迎光临,"+ URL );
意味着给了我空值
最佳答案
你的代码
String mTitle;
String URL = "http://api1.sfsfsffsf.com/dev/categories/?fields=&categoryid=" + mTitle + "&access_token=bfb787a";
static String KEY_CATEGORY = "category";
// public static final String URL_Address=null;
static final String KEY_TITLE = "categoryName";
static final String KEY_NAME ="categoryId";
static final String KEY_SUBCATE = "categoryId";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subcate);
Bundle b = getIntent().getExtras();
mTitle = b.getString(KEY_SUBCATE);
TextView grandtotal = (TextView) findViewById(R.id.subcate);
grandtotal.setText("Welcome ," + mTitle );
错误:
String URL = "http://api1.sfsfsffsf.com/dev/categories/?fields=&categoryid=" + mTitle + "&access_token=bfb787a";
正确代码:
String mTitle;
String URL;
static String KEY_CATEGORY = "category";
// public static final String URL_Address=null;
static final String KEY_TITLE = "categoryName";
static final String KEY_NAME ="categoryId";
static final String KEY_SUBCATE = "categoryId";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subcate);
Bundle b = getIntent().getExtras();
mTitle = b.getString(KEY_SUBCATE);
URL = "http://api1.sfsfsffsf.com/dev/categories/?fields=&categoryid=" + mTitle + "&access_token=bfb787a";
TextView grandtotal = (TextView) findViewById(R.id.subcate);
grandtotal.setText("Welcome ," + mTitle );
您的 mTitle 之后将被初始化,因此您只需要在此之后初始化 URL。
关于java - 在java中调用字符串值到URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064791/