2013-03-17 42 views
0

您好我是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)); 
      } 


     } 
     } 

     } 
} 
+0

按Ctrl-Shift-F来设置你的源代码格式,这是一团糟,一旦你的代码格式正确,你的问题就会变得更加明显 – 323go 2013-03-17 16:33:00

+0

道歉,我已经改变了它 – Nick 2013-03-17 16:35:55

+0

我会说你坏了建议在AsyncTask中尝试进行网络交互是注定的。 – 2013-03-17 16:39:36

回答

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 ""; 
     } 
    } 
+0

我试过代码 - 有一个错误的结果' String result = convertStreamToString(response.getEntity()。getContent());'我试着将它改为result1并编译,但仍然没有数据。 – Nick 2013-03-17 19:21:30

+0

@Nick更新了,不要把它改成'result1',删除'String' – Blundell 2013-03-17 19:32:33

+0

Thankyou我改变了它,但数据仍然丢失 – Nick 2013-03-17 19:40:39

1

在开始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(); 
    } 
}