2015-04-18 5 views
1

В моем URL я хочу, чтобы заменить «{камеру}» с «домом»Как заменить одно слово в URL

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);}}} 

если я передать этот адрес непосредственно (http://10.11.32.199/congressionaldirectory/services/getbillsdetailsbychamber?chamber=house&app_code=125143649) его рабочие штрафа пожалуйста помочь мне

ответ

4
private public String url ="http://10.11.32.199/congressionaldirectory/services/getbillsdetailsbychamber?chamber={chamber}&app_code=125143649"; 

url=url.replace("{chamber}","house"); 

Вам просто нужно использовать строку замены потому что он является статическим и частным, поэтому он будет работать плавно.

+0

Можно ли отправить этот «дом» URL-адресу с помощью JSON. – PeddiRaju

2

Вы можете использовать string.replace:

private static String url ="http://10.11.32.199/congressionaldirectory/services/getbillsdetailsbychamber?chamber={chamber}&app_code=125143649"; 

String modified_url = url.replace("{chamber}","house"); 
+0

Почему использовать другую строку, когда мы можем использовать один и тот же :). –

Смежные вопросы