java - 使用 Eclipse 连接到 wamp 服务器的 IOException 错误?

标签 java android eclipse apache wampserver

我在使用我用 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/

相关文章:

java - 带 jar 的包和带类的包有什么区别?

android - 如何覆盖 Android 应用程序中的配置?

java - 创建 ScriptC 时 RenderScript 崩溃

java - 迁移到Android Studio : Class resolved in editor,但构建时找不到

java - jface 中最后一列变得过大

java - 使用Java进行体育比赛总结

java - 有没有办法强制程序留在 RAM 中

java - "Un-nesting"我的 Java 中的 Try/Catch block

android - 如何创建 3 个连接的按钮?

eclipse - 无法安装 Subversive SVN Team Provider