我在使用我用 eclipse 制作的 android 程序连接到 WAMP 服务器
时遇到问题....
这是我的课:
public class Webservice {
public static String readurl(String url)
{
try {
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
HttpResponse response = client.execute(method);
InputStream inputStream = response.getEntity().getContent();
String resault = ConvertInputStream(inputStream);
return resault;
}
catch (ClientProtocolException e) {
//e.printStackTrace();
Log.i("Log", "Protocol");
}
catch (IOException e) {
//e.printStackTrace();
Log.i("Log", "IOException");
}
return "read";
}
private static String ConvertInputStream(InputStream inputStream)
{
try
{
BufferedReader reader = new BufferedReader(new
InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
builder.append(line);
}
return builder.toString();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
}
这是我的 Activity 代码:
public class NotesActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String resault = Webservice.readurl("http://localhost/note-server");
Log.i("Log", "Resault: " + resault);
当我运行时,由于我类(class)中 IOException 下的“Log”,它会给我“IOException”,最后给我“Result: read”!!!!!
我该如何解决?
最佳答案
如果您在模拟器上运行,那么您应该使用 10.0.2.2
而不是 localhost
。
参见 this发布。
关于java - 使用 Eclipse 连接到 wamp 服务器的 IOException 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45507860/