This question already has answers here:
replace String with another in java
(5个答案)
4年前关闭。
我想在我的网址中将“ {chamber}”替换为“ house”
如果我直接通过此URL(http://10.11.32.199/congressionaldirectory/services/getbillsdetailsbychamber?chamber=house&app_code=125143649),它的工作正常
请帮助我
您只需要使用字符串替换功能,因为它是静态的和私有的,因此可以正常工作。
(5个答案)
4年前关闭。
我想在我的网址中将“ {chamber}”替换为“ house”
public class MainActivity extends Activity {
private static String url ="http://10.11.32.199/congressionaldirectory/services/getbillsdetailsbychamber?chamber={chamber}&app_code=125143649";
String jsonStr;
TextView tv;
private static final String TAG_CHAMBER = "chamber";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView1);
new GetContacts().execute(url);
}
private class GetContacts extends AsyncTask<String,String,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
HttpClient httpclient=new DefaultHttpClient();
URI u;
try {
u = new URI(params[0]);
HttpPost HG=new HttpPost(u);
ArrayList<NameValuePair> al=new ArrayList<NameValuePair();
al.add(new BasicNameValuePair(TAG_CHAMBER,"house"));
HG.setEntity(new UrlEncodedFormEntity(al));
HttpResponse response=httpclient.execute(HG);
jsonStr = EntityUtils.toString(response.getEntity());
Log.d("Response: ", "> " + jsonStr);
} catch (URISyntaxException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tv.setText(jsonStr);}}}
如果我直接通过此URL(http://10.11.32.199/congressionaldirectory/services/getbillsdetailsbychamber?chamber=house&app_code=125143649),它的工作正常
请帮助我
最佳答案
private public String url ="http://10.11.32.199/congressionaldirectory/services/getbillsdetailsbychamber?chamber={chamber}&app_code=125143649";
url=url.replace("{chamber}","house");
您只需要使用字符串替换功能,因为它是静态的和私有的,因此可以正常工作。
10-07 14:02