我是android的初学者。我想存储来自网络链接的数据.. 我会给你链接..它只有一个值。
“https://api.thingspeak.com/channels/305276/fields/1/24?api_key=KD5IR14FQW33TM4Y”
我想将链接中的值分配给我的android程序中的变量。我正在尝试但是我没有得到..请帮助我..
这是我试过的代码..
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.*;
import java.io.*;
public class urll {
public static String data(int i) {
URL oracle = null;
String a = null;
try {
oracle = new URL("https://api.thingspeak.com/channels/305276/fields/1/24?api_key=KD5IR14FQW33TM4Y");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection yc = null;
try {
yc = oracle.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
String inputLine;
try {
while ((inputLine = in.readLine()) != null){
a=inputLine;
}
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println(inputLine);
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return a;
}
}