Android AsyncTask连接拒绝模拟器+ Mamp

时间:2013-06-07 09:37:49

标签: android android-asynctask connection httpclient

我想在我的Android应用程序中从本地Web服务器读取JSon。

这是我的代码:

public class MainActivity extends Activity implements OnItemClickListener {

HttpClient client;
JSONObject json;

public final static String EXTRA_MESSAGE = "com.example.gestionformations.MESSAGE";
final static String URL = "http://10.0.2.2/apis/general/get_categories/";
final static String LOG = "gestionFormations.log";
ListView conferenceListe;
ListView coursListe;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    client = new DefaultHttpClient();
    Log.i(LOG,"Create");
    new Read().execute("libcategorie");
}

public JSONObject getCategories() throws ClientProtocolException, IOException, JSONException{
    StringBuilder url = new StringBuilder(URL);
    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if(status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray catArray = new JSONArray(data);
        JSONObject lesCategories = catArray.getJSONObject(0);
        //Log.i(LOG,"JsonObject : "+ lesCategories.toString());
        return lesCategories;
    }else{
        Log.i(LOG,"Erreur getCategories");
        return null;
    }
}

public class Read extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {
        try {
            json = getCategories();
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        Log.i(LOG,result);
    }

}
}

我的Manifest.xml看起来像这样:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<uses-sdk
   android:minSdkVersion="8"
   android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET"/>

<application>
...
</application>

</manifest>

这是错误日志:

06-07 09:18:08.965: W/System.err(3565): org.apache.http.conn.HttpHostConnectException: Connection to `http://10.0.2.2` refused
06-07 09:18:08.965: W/System.err(3565):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
06-07 09:18:09.115: W/System.err(3565):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-07 09:18:09.145: W/System.err(3565):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-07 09:18:09.145: W/System.err(3565):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-07 09:18:09.175: W/System.err(3565):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-07 09:18:09.175: W/System.err(3565):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-07 09:18:09.225: W/System.err(3565):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-07 09:18:09.335: W/System.err(3565):     at com.example.gestionformations.MainActivity.getCategories(MainActivity.java:88)
06-07 09:18:09.685: W/System.err(3565):     at com.example.gestionformations.MainActivity$Read.doInBackground(MainActivity.java:113)
06-07 09:18:09.735: W/System.err(3565):     at com.example.gestionformations.MainActivity$Read.doInBackground(MainActivity.java:1)
06-07 09:18:09.825: W/System.err(3565):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-07 09:18:09.895: W/System.err(3565):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-07 09:18:09.925: W/System.err(3565):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-07 09:18:09.925: W/System.err(3565):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-07 09:18:09.975: W/System.err(3565):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-07 09:18:09.995: W/System.err(3565):     at java.lang.Thread.run(Thread.java:856)
06-07 09:18:09.995: W/System.err(3565): Caused by: java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect failed: ECONNREFUSED (Connection refused)
06-07 09:18:10.275: W/System.err(3565):     at libcore.io.IoBridge.connect(IoBridge.java:114)
06-07 09:18:10.275: W/System.err(3565):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-07 09:18:10.315: W/System.err(3565):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-07 09:18:10.345: W/System.err(3565):     at java.net.Socket.connect(Socket.java:842)
06-07 09:18:10.475: W/System.err(3565):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-07 09:18:10.475: W/System.err(3565):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
06-07 09:18:10.524: W/System.err(3565):     ... 15 more
06-07 09:18:10.524: W/System.err(3565): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
06-07 09:18:10.534: W/System.err(3565):     at libcore.io.Posix.connect(Native Method)
06-07 09:18:10.534: W/System.err(3565):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
06-07 09:18:10.545: W/System.err(3565):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-07 09:18:10.545: W/System.err(3565):     at libcore.io.IoBridge.connect(IoBridge.java:112)

0 个答案:

没有答案