0

こんにちは私はAndroidプログラミングに不慣れで、ネットワーク接続を確立するためにAsyncTaskを使用するようにアドバイスされました。doInBackgroundに追加する前に機能していた領域でエラーが発生します。

(例は解決できません)-キャッチセクション

およびonPostExecute:

トークン")"の構文エラー、; 期待される-(文字列結果)

(Toast型のメソッドmakeText(Context、CharSequence、int)は、引数(LongOperation、String、int)には適用されません)-Toast.makeTextの場合

これらのエラーは、コードを配置した場所と関係がありますか?私のコードを適応させる答えを歓迎します。

package com.example.clearlight;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;

import java.net.URL;

import org.apache.http.client.ResponseHandler;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.StrictMode;
import android.util.Log;

public class MainActivity extends Activity {

    TextView txt;

    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


        setContentView(R.layout.relative);

        class LongOperation extends AsyncTask<String, Void, String> { 

            @Override
            protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            URL url = null;

            DefaultHttpClient httpclient = null;

            try {

            String registrationUrl = "http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
            url = new URL(registrationUrl);

            HttpGet getRequest = new HttpGet(registrationUrl);
            ResponseHandler<String> handler = new BasicResponseHandler();
            httpclient = new DefaultHttpClient();
            // request data from server
            String result = httpclient.execute(getRequest, handler);
            Log.d("MyApp", "Data from server is "+ result);}

            catch (Exception ex) {Log.e("error",ex.toString());
            ex.printStackTrace();

            }


            @Override
            protected void onPostExecute(String result) 
            {               
              TextView text1 = (TextView) findViewById(R.id.text);

            //Sets the new text to TextView (runtime click event)//*******
            text1.setText("Light Data= " + result);

            Toast.makeText(this, "Light Data:" + result, Toast.LENGTH_SHORT).show(); //MESSAGE BOX
            //txtMessage.setText(String.valueOf(msg1) + "  " + String.valueOf(msg2));  
            }


        } 
        }

      }
}
4

2 に答える 2

1

削除する

return null; 

メソッドを開始しdoInBackgroundてこのメ​​ソッドを閉じるときに、閉じ中括弧を挿入し、コードの最後の中括弧を削除します。

@Override
protected String doInBackground(String... params) {
    URL url = null;
    DefaultHttpClient httpclient = null;
    try {
        String registrationUrl = "http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
        url = new URL(registrationUrl);

        HttpGet getRequest = new HttpGet(registrationUrl);
        ResponseHandler<String> handler = new BasicResponseHandler();
        httpclient = new DefaultHttpClient();
        // request data from server
        String result = httpclient.execute(getRequest, handler);
        Log.d("MyApp", "Data from server is "+ result);
    }
    catch (Exception ex) {Log.e("error",ex.toString());
        ex.printStackTrace();
    }
}
于 2013-03-17T16:30:53.770 に答える
0

これらは単なる構文エラーであり、とは関係ありませんAsyncTask

コードをフォーマットする(Ctrl-Shift-F)と、この問題の修正がはるかに簡単になります。

メソッドの直後に中括弧がないため、を最初から最後doInBackgroundに移動する必要があります。return null

    @Override
    protected String doInBackground(String... params) {
        URL url = null;

        DefaultHttpClient httpclient = null;

        try {

            String registrationUrl = "http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
            url = new URL(registrationUrl);

            HttpGet getRequest = new HttpGet(registrationUrl);
            ResponseHandler<String> handler = new BasicResponseHandler();
            httpclient = new DefaultHttpClient();
            // request data from server
            String result = httpclient.execute(getRequest, handler);
            Log.d("MyApp", "Data from server is " + result);
        } catch (Exception ex) {
            Log.e("error", ex.toString());
            ex.printStackTrace();

        }

       return null;
    } // THIS HERE

    @Override
    protected void onPostExecute(String result) {
        TextView text1 = (TextView) findViewById(R.id.text);

        //Sets the new text to TextView (runtime click event)//*******
        text1.setText("Light Data= " + result);

        Toast.makeText(this, "Light Data:" + result, Toast.LENGTH_SHORT).show(); //MESSAGE BOX
        //txtMessage.setText(String.valueOf(msg1) + "  " + String.valueOf(msg2));
    }

私はあなたのコードを調べました、そして私は以下がうまくいくかもしれないと思います(私はStackOverflowでこれをしただけですが、それはあなたが間違っているところのより多くの手がかりをあなたに与えるかもしれません:

        @Override
        protected String doInBackground(String... params) {
            String result = "Failed";
            try {
                String registrationUrl = "http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
                // request data from server
                URL url = new URL(registrationUrl);
                HttpGet getRequest = new HttpGet(registrationUrl);
                DefaultHttpClient httpclient = new DefaultHttpClient();

                HttpResponse response = httpclient.execute(getRequest);
                result = convertStreamToString(response.getEntity().getContent());
                Log.d("MyApp", "Data from server is " + result);
            } catch (Exception ex) {
                Log.e("MyApp", "Failed to load light data", ex);
            }

           return result;
        } // THIS HERE

        @Override
        protected void onPostExecute(String result) {
            TextView text1 = (TextView) findViewById(R.id.text);
            text1.setText("Light Data= " + result);

            Toast.makeText(MainActivity.this, "Light Data:" + result, Toast.LENGTH_SHORT).show();
        }

     public String convertStreamToString(InputStream inputStream) throws IOException {
        if (inputStream != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),1024);
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                inputStream.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }
于 2013-03-17T16:33:44.693 に答える