サーバーと通信する場合のコード例を次に示します
// this method takes URL as input and returns JSON data
//parent class name is "NewWebHelper.java"
public String getResult(String url) {
Log.v(TAG, "Final Requsting URL is : :"+url);
String line = "";
String responseJsonData = null;
try {
StringBuilder sb = new StringBuilder();
String x = "";
URL httpurl = new URL(url);
URLConnection tc= httpurl.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(tc.getInputStream()));
if(in !=null){
while ((line = in.readLine()) != null) {
sb.append(line + "\n");
x = sb.toString();
}
responseJsonData = new String(x);
}
}
catch (UnknownHostException uh){
Log.v("NewWebHelper", "Unknown host :");
uh.printStackTrace();
}
catch (FileNotFoundException e) {
Log.v("NewWebHelper", "FileNotFoundException :");
e.printStackTrace();
}
catch (IOException e) {
Log.v("NewWebHelper", "IOException :");
e.printStackTrace();
}
catch (Exception e) {
Log.v("NewWebHelper", "Exception :");
e.printStackTrace();
}
return responseJsonData;
}
これを再グレードするのに問題がある場合はお知らせください。