2017-08-04 98 views
1

我有连接到WAMP server与我,我与日食的android做程序的问题....用eclipse连接到wamp服务器的IOException错误?

这是我的课:

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; 
} 

} 

这是我的活动代码:

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); 

当我运行时,由于我的类下的IOException异常导致“IOException”,并最终给我“Result:read”!!!!!

我该如何解决这个问题?

+0

使用'e.printStackTrace();'你可以看到异常行 –

+0

首先添加完整的堆栈跟踪。其次,你想解决什么问题?您在Webservice中获得的异常或写入日志的活动,即使Webservice处于异常状态? –

+0

我在模拟器上运行,但重点是,当我尝试连接到一些网站,如谷歌和雅虎它工作正常,但我只是不连接到我的Wamp服务器! –

回答

1

如果你正在模拟器上运行,那么你应该使用10.0.2.2而不是localhost

请参阅this后。