1

I have a problem with connecting to WAMP server with my android program that I made with eclipse....

This is my class:

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

}

And this is my activity code:

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

When I run, it gives me "IOException" because of the "Log" in my class under the IOException, and at the end give me "Result: read"!!!!!

How can I fix that?

Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
Hooman BZ
  • 53
  • 1
  • 8
  • Use `e.printStackTrace();` and you can see the line with exception – Stanislav Bondar Aug 04 '17 at 13:20
  • First of all add the complete stack trace. Second, what do you want to fix? The exception you get in Webservice or the activity who write the log, even if the Webservice was in exception? – anemomylos Aug 04 '17 at 13:21
  • I'm running on an emulator but the point is, when i try to connect to some sites like google and yahoo it works OK but i just doesn't connect to my wamp server!!! – Hooman BZ Aug 04 '17 at 13:38

1 Answers1

1

If you are running on an emulator then you should use 10.0.2.2 instead of localhost.

See this post.

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33