问题
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?
回答1:
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?
回答2:
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
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/11614295/connection-refused-when-trying-to-acces-local-webservice-using-android