2014-09-29 175 views
2

有人可以解釋爲什麼以及如何解決此錯誤消息,我在編程或Android時在Eclipse中獲取?Android Eclipse:HTTP無法解析爲類型

HTTP不能被解析爲一個類型

代碼示例如下。

try { 
       // Call class "HTTP" passing an URL using Async "execute" 
       // Save returned string in "result" 
       String result = new HTTP().execute("http://domain.com/file.php?ID=1&R=T").get(); 

       Log.i("EmonLog", "Result: "+result); 

       result = result.replaceAll("\"",""); 
       TextView powerval = (TextView) findViewById(R.id.information_message); 
       powerval.setText("Result: " + result); 
      } catch (Exception e) { 
       Log.i("EmonLog", "Error: "+e); 
      } 

的程序編譯和完美的作品,但我得到這個錯誤,在HTTP下的紅線,我想知道爲什麼,以及如何解決它。

編輯...添加了一些信息。這是execute調用的線程。

class HTTP extends AsyncTask<String, Void, String> 
{ 
    @Override 
    protected String doInBackground(String... params) { 
     String result = ""; 
     try { 
      // Get the first parameter of the "params" which has been related to "String" 
      String urlstring = params[0]; 
      // Log the string that contains the url 
      Log.i("EmonLog", "HTTP Connecting: "+urlstring); 
      // Convert the String into an actual URL 
      URL url = new URL(urlstring); 
      // Make the HTTP connection to the Internet 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
      // Once the connection is done, try to store the information received into "reader" 
      try { 
       InputStream reader = new BufferedInputStream(urlConnection.getInputStream()); 
       // Create a new String which will extract and contain the information received into the Buffered Input Stream 
       String text = ""; 
       int i = 0; 
       while((i=reader.read())!=-1) 
       { 
        text += (char)i; 
       } 
       Log.i("EmonLog", "HTTP Response: "+text); 
       result = text; 

      } catch (Exception e) { 
       Log.i("EmonLog", "HTTP Exception: "+e); 
      } 
      finally { 
       Log.i("EmonLog", "HTTP Disconnecting"); 
       urlConnection.disconnect(); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.i("EmonLog", "HTTP Exception: "+e); 
     } 

     return result; 
    } 
} 

我進口:

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

非常感謝您的幫助。

+0

顯示您的請求代碼。 – 2014-09-29 07:13:25

+0

我不明白。我在兩個地方使用這個。 1)裏面的按鈕按這樣的bt_button_HVAC25Vmax.setOnClickListener(新的View.OnClickListener(){(2)和這樣的線程內public void run(){ – Serge 2014-09-29 07:15:02

+0

我運行你的代碼,一切工作正常。可能有些問題 – 2014-09-29 08:39:42

回答

2

您不能將HTTP用作AsyncTask的名稱。

正如你已經有

org.apache.http.protocol.HTTP 

爲HTTP。

因此,請將您的AsyncTask重命名爲其他內容。

可能,

class HTTPTask extends AsyncTask<String, Void, String> 

就不夠好。

+0

但是使用完整的軟件包名稱應該可以解決這個問題,不是嗎? – shyam 2014-09-29 09:58:28

+0

如果OP使用正確,但它是否是好的做法?@shyam – 2014-09-29 09:59:01

+0

非常感謝,似乎解決了錯誤消息。它正如你所指出的那樣,也是在「String result = new HTTPTask()。execute」中。現在我只需分析爲什麼這種方法不是最合適的方法作爲奧列格的e請求通知我。對不起,我不能給予積極的反饋,因爲我是新的,但沒有聲望。 – Serge 2014-09-29 10:01:27