web-dev-qa-db-ja.com

Android HTTP Get

いくつかのフォーラム投稿を確認しましたが、問題の答えが見つかりません。 PHPファイルから応答を取得しようとしています。 phpファイルが機能しています。問題は、Androidアプリがリクエストを実行しないことです。コードの2つの例と、テキストビューで取得した結果を次に示します。

public void changText(View view) {
    TextView textv = (TextView)findViewById(R.id.textview1);
    textv.setText("Text Has Been Changed");
    BufferedReader in = null;
    String data = null;

    try{
           HttpClient httpclient = new DefaultHttpClient();

           HttpGet request = new HttpGet();
           URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
           request.setURI(website);
           HttpResponse response = httpclient.execute(request);
           in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

           textv.append(" Connected ");
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
       }


   }

TextViewの読み取り:テキストが変更されました

    public void changText(View view) {
    TextView textv = (TextView)findViewById(R.id.textview1);
    textv.setText("Text Has Been Changed");
    BufferedReader in = null;
    String data = null;

    try{
           HttpClient httpclient = new DefaultHttpClient();

           HttpGet request = new HttpGet();
           URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
           request.setURI(website);
           //HttpResponse response = httpclient.execute(request);
           //in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

           textv.append(" Connected ");
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
       }


   }

TextViewの読み取り:テキストが変更されました接続されています

このマニフェストには次のものがあります。

<uses-permission Android:name="Android.permission.INTERNET" />

エラーログに次のメッセージが表示されます。http接続のエラーAndroid.os.NetworkOnMainThreadException

どんな助けでもいただければ幸いです。

4
user908759

Androidはおそらくあなたのリクエストをうまく実行しています。サーバーから返されたデータを無視しているようです。たとえば、次のようなことができます。

_public void changText(View view) {
    TextView textv = (TextView)findViewById(R.id.textview1);
    textv.setText("Text Has Been Changed");
    BufferedReader in = null;
    String data = null;

    try{
           HttpClient httpclient = new DefaultHttpClient();

           HttpGet request = new HttpGet();
           URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
           request.setURI(website);
           HttpResponse response = httpclient.execute(request);
           in = new BufferedReader(new InputStreamReader(
                   response.getEntity().getContent()));

           // NEW CODE
           String line = in.readLine();
           textv.append(" First line: " + line);
           // END OF NEW CODE

           textv.append(" Connected ");
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
       }
}
_

サーバーがJSONオブジェクトを返しているように見えるので、応答全体を読み取り、解析し(new JSONArray(response)を使用)、関連するフィールドを抽出するなど、よりインテリジェントな処理を実行することをお勧めしますが、上記のコード少なくともAndroidがクエリを実行していることを確認します。

編集:報告した例外から、メインUIスレッドでコードを実行しているようです。禁止されている(そしてその前に嫌われていた)API11の時点で。これを修正する方法に関するいくつかの記事があります。ガイドトピック 無痛スレッド およびチュートリアル ここ および ここ を参照してください。詳細については、 このスレッド も参照してください。

13
Ted Hopp
 private InputStream downloadUrl(final URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(NET_READ_TIMEOUT_MILLIS /* milliseconds */);
    conn.setConnectTimeout(NET_CONNECT_TIMEOUT_MILLIS /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    return conn.getInputStream();
}
0
Raj008