How to create a string resource from an external source?

0

I'm trying to access a .php file in the localhost:

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try  {
                    System.err.println("It's working ok?");
                    String url = "http://192.168.0.112/hlsystems/api/fetch.php";
                    String charset = "UTF-8";  // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name()
                    String param1 = "value1";
                    String param2 = "value2";
                    String query = "";

                    try {
                        query = String.format("param1=%s&param2=%s",
                                URLEncoder.encode(param1, charset),
                                URLEncoder.encode(param2, charset));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    URLConnection connection = null;
                    try {
                        connection = new URL(url + "?" + query).openConnection();
                        connection.setRequestProperty("Accept-Charset", charset);
                        InputStream response = connection.getInputStream();
                        System.err.println(response.read());
                        Toast.makeText(mainContext, response.read(),
                                Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                        Toast.makeText(mainContext, e.toString(),
                                Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        thread.start();

When I run it, I get the following error:

W/System.err: android.content.res.Resources$NotFoundException: String resource ID #0x22

How am I supposed to previously define a string inside the XML file, if I'm retrieving it from an external source / a webserver?

Full log:

W/System.err: 123
W/ResourceType: No package identifier when getting value for resource number 0x00000022
W/System.err: android.content.res.Resources$NotFoundException: String resource ID #0x22
W/System.err:     at android.content.res.Resources.getText(Resources.java:250)
W/System.err:     at android.widget.Toast.makeText(Toast.java:268)
W/System.err:     at com.hlsystems.ericsonwillians.saltodepirapora.MainActivity$2.run(MainActivity.java:89)
W/System.err:     at java.lang.Thread.run(Thread.java:841)
java
android
asked on Stack Overflow Apr 3, 2018 by Ericson Willians • edited Apr 3, 2018 by Phantômaxx

1 Answer

2

response.read() returns an int, a single byte of the response.

You need to convert the stream response to a full String before you Toast it.

The makeText method is overloaded to accept R.string resource IDs as a second parameter, which are read only assets, and you cannot create at runtime

If you only care about that one byte, see

Displaying integer on toast

answered on Stack Overflow Apr 3, 2018 by OneCricketeer • edited Apr 3, 2018 by OneCricketeer

User contributions licensed under CC BY-SA 3.0