我正在尝试访问localhost中的.php文件:
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¶m2=%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();
当我运行它时,我收到以下错误:
W/System.err: android.content.res.Resources$NotFoundException: String resource ID #0x22
如果我从外部源/网络服务器检索字符串,我该如何预先在XML文件中定义字符串?
完整日志:
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)
答案 0 :(得分:2)
response.read()
返回一个int,即响应的单个字节。
在Toast之前,您需要将流响应转换为完整的String。
重载makeText方法以接受R.string
资源ID作为第二个参数,它们是只读资源,并且您无法在运行时创建
如果您只关心那个字节,请参阅