0

I am creating a server built in Visual Basic 2010 and that program can insert/update/delete to a database that I use. I created a local Web Service that is used to synchronize the database on the server with the database in Android.

I use the following Android code :

package com.zelacroix.bukumenu;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.params.*;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class Sinc extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sinc);
        Toast.makeText(getApplicationContext(), getKategori(), 5).show();
    }

    public String getKategori(){
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 60000);
        HttpConnectionParams.setSoTimeout(httpParameters, 60000);
        HttpClient client=new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost("http://192.168.1.2:1924/TugasAkhir/Service.asmx/getKategori");
        HttpResponse response;
        String result="";
        try
        {
            response=client.execute(httpPost);
            HttpEntity entity= response.getEntity();
            DataHandler dataHandler = new DataHandler();
            if (entity!=null)
            {
                InputStream instream = entity.getContent();
                result = dataHandler.convertStreamToString(instream);
                instream.close();
            }
        } catch (ConnectTimeoutException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.toString(), 100).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), e.toString(), 100).show();
        }
        return result;
    }


}

I get an error : org.apache.http.conn.httphostconnectexception connection to ``http://192.168.1.2:1924..... refused

For your information.. This code runs successfully when I'm using the emulator and change the IP address to 10.0.2.2.

This code also run successfully when I access a hosted online web service. It fails only when I run the web service as local and try to access it with an Android device using my laptop's IP (192.168.1.2).

I am using WIFI.

How can I fix this error?

Eran
  • 387,369
  • 54
  • 702
  • 768
Michael
  • 157
  • 1
  • 4
  • 10

4 Answers4

2

Try adding:

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Does this help?

Mxyk
  • 10,678
  • 16
  • 57
  • 76
  • 1
    Add it in onCreate(). That should be fine. And yeah, Mike could very well be right. If you're targeting or building on a newer phone, if your HTTP requests aren't done on a separate thread, the connection will be refused by Android. – leenephi Jul 23 '12 at 14:43
  • when i add it on onCreate() it got 2 errors :'( it sound "strictmode cannot be resolved to a type" – Michael Jul 23 '12 at 14:56
  • Did you import it? Try Ctrl + Shift + O (or for a Mac, Cmd + Shift + O). – Mxyk Jul 23 '12 at 15:27
  • There is a chance you're developing for an API level 9 or lower (thus Eclipse won't recognize the newer code), but you're running it on a higher API level device. Trying setting your target SDK higher (eg 15) for development. – Mxyk Jul 23 '12 at 17:00
  • first i developed that code using API level 8 or froyo 2.2, but i run my application in ICS 4.0.3, so far there is no problem.. TCP communication is successfull.. but now i'm facing this problem.. when i change the SDK to target API level 11 (highest i have) in my manifest got warning "Attribute minSdkVersion (11) is higher than the project target API level (8)" how to solved this.. even i change the SDK to level 11, the code above stil got two error :'( – Michael Jul 24 '12 at 01:34
  • Mike is right, add this piece of code `android.os.Build.VERSION.SDK_INT > 9` in `protected void onCreate(Bundle savedInstanceState)` for your activity. Also you must check if you have a firewall exception for the port in your operating system, test if you can browse it. Prefer to use http://192.168.0.XXX/your-page-here and check your manifest file: `` – Junior Mayhé Mar 10 '14 at 23:46
0

check your laptop ip and your device ip.

it should be in same sub net mask

The laptop and mobile(android) device ip must be 192.168.X.X

satheesh
  • 187
  • 1
  • 4
  • 14
  • it is on the same class.. my laptop IP is 192.168.1.2 and my Android got 192.168.1.4 – Michael Jul 23 '12 at 14:28
  • i had the same problem. so did debugging in emulator. after that i installed apk in device. – satheesh Jul 23 '12 at 14:34
  • debugging in same system will work(like emulator). if you want to debug from outside the system. you have to modify the configuration. The easier way is can open the service in browser put the possible values then you can debug the code. – satheesh Jul 24 '12 at 13:23
  • when i debugging with emulator i used 10.0.2.2:1924 and its work.. when i used wifi, the code above can access web service that hosted online perfectly.. my problem there.. is when i run my web service (.asmx) locally so it has address like http://localhost:1924/TugasAkhir/Service.asmx and then i'm trying to access that web service using my code above via wifi, but got connection refused :( – Michael Jul 25 '12 at 02:10
  • open the service in mobile browser. if you have same issue. check the firewall. – satheesh Jul 26 '12 at 05:06
0

finally its solved.. the problem is visual studio 2010 so complicated in setting the IIS, then i try to developt my web service on Visual studio 2008 and IIS work fine! the Android can access the web service without connection refused.

Michael
  • 157
  • 1
  • 4
  • 10
0

Ran into this problem but the solution was rather something some would encounter by familiarity.

Make your the <uses-permission android:name="android.permission.INTERNET"/> is on your Manifest.

lock
  • 6,404
  • 18
  • 58
  • 76