有人可以解释为什么以及如何修复我在Eclipse编程或Android时遇到的错误消息?
无法将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下的红线,我想了解原因,以及如何解决它。
编辑...添加了更多信息。这是执行调用的线程。
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 :(得分:2)
您不能将HTTP用作AsyncTask的名称。
你已经有了
org.apache.http.protocol.HTTP
作为HTTP。
请将您的AsyncTask重命名为其他内容。
可能是,
class HTTPTask extends AsyncTask<String, Void, String>
会很好。