1

I want to use string variable in place of ip address in url call. I want to make login with ip address and port number in application. then I want to save this ip address in shared preferences and then in url call i want to use that store ip address in shared preferences

I get the ip address of shared preferences like this.

SharedPreferences pref = getActivity().getSharedPreferences("MyPref", MODE_PRIVATE);


    ip= pref.getString("key_ip", null);           // getting Float

    Log.e("ip: ", "> " + ip);

Then i call the url using method like this.

public static String off33() {
        StringBuffer stringBuffer = new StringBuffer("");
        BufferedReader bufferedReader = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet();

            URI uri = new URI("http://10.1.1.82:80/outlet?3=ON");


            httpGet.setURI(uri);
            httpGet.addHeader(BasicScheme.authenticate(
                    new UsernamePasswordCredentials("admin", "kirti123"),
                    HTTP.UTF_8, false));

            HttpResponse httpResponse = httpClient.execute(httpGet);
            InputStream inputStream = httpResponse.getEntity().getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream));

            String readLine = bufferedReader.readLine();
            while (readLine != null) {
                stringBuffer.append(readLine);
                stringBuffer.append("\n");
                readLine = bufferedReader.readLine();
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO: handle exception
                }
            }
        }
        return stringBuffer.toString();
    }

so, I want to use ip string in place of url in method 10.1.1.82 means i want to use like this.

            URI uri = new URI("http://ip:80/outlet?3=ON");

ip is a string variable and in this variable i get 10.1.1.82

so how i cant use this?

2 Answers 2

1

Just concatenate the Strings

URI uri = new URI("http://" + ip + ":80/outlet?3=ON");
Sign up to request clarification or add additional context in comments.

1 Comment

Thnx a lot...!!
0

You can also do it this way:

String base = "http://%1$s:80/outlet?3=ON";
String address = String.format(base,ip);
URI uri = new URI(address);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.