私はAndroid開発に不慣れです。アプリをPHPにテスト接続したいと思います。私はたくさんググったが、それらはすべて複雑だった。
まず、接続を学習し、「Hello world」のような単純なテキストを1つだけ取得して、Androidアプリに印刷したいと思います。
例または簡単な例のリンクは何でしょうか?
作業を開始する例を次に示します。単なるコピー&ペーストですが、機能するはずです。
リクエストの出力はmyStringに保存されます。
ああ、GUIスレッドでそのような操作を実行しないでください。それらを別のスレッドで実行します。
// An the Android manifest, add:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
String myString;
String url = "http://www.yoursite.com/yourphpscript.php";
HttpClient httpclient = new DefaultHttpClient();
HttpGet request;
try {
request = new HttpGet(new URI(url));
request.addHeader("User-Agent", "Android");
HttpResponse response = httpclient.execute(request);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
myString = out.toString();
}
}
catch (URISyntaxException e1) {
e1.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
Java Code Geeksには、GSON(ライブラリ)を使用したJSONの解析、Gsonチュートリアルを使用したAndroidJSONの解析に関する優れた記事があります。これにより、必要なすべての接続ビットと、Webサービスとして「helloworld」の提供を開始した場合にソリューションを拡張する方法がわかります。