2014-09-29 45 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