您好我是Android編程的新手,並建議使用AsyncTask來建立我的網絡連接。在閉鎖段在AsyncTask中的錯誤
和onPostExecute -
(當然不能被解析):
語法我之前我加入他們到doInBackground這是工作區接收錯誤令牌錯誤「)」,「; expected-爲(字符串結果)
(方法makeText(上下文,CharSequence的,INT)在類型吐司是 不適用於參數(LongOperation,字符串,整數)) - 用於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)); } } } } }
按Ctrl-Shift-F來設置你的源代碼格式,這是一團糟,一旦你的代碼格式正確,你的問題就會變得更加明顯 – 323go 2013-03-17 16:33:00
道歉,我已經改變了它 – Nick 2013-03-17 16:35:55
我會說你壞了建議在AsyncTask中嘗試進行網絡交互是註定的。 – 2013-03-17 16:39:36