连接到android中的localhost时连接被拒绝

时间:2013-06-17 07:29:20

标签: android

  

朋友您好我正在尝试将我的Android应用程序连接到localhost并使用json在listview中显示名称但发生了愚蠢的错误请帮我下面是我的代码和错误。

public class MainActivity extends Activity {

Spinner spin;
InputStream is = null;

String ip = "http://localhost/Demo/select.php";
String line = null;
String result = null;

List<String> list;

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

    spin = (Spinner) findViewById(R.id.spinner1);

    new JSONDemo().execute();

}

public class JSONDemo extends AsyncTask<Void,Void,String>{  

    @Override
    protected String doInBackground(Void... params) {
        try{
        webserviceCall();
        }
        catch(Exception e){
            Log.v("error", e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

        try{
            setData();
            }
            catch(Exception e){
                Log.v("error", e.toString());
            }           
    }



}

private void webserviceCall() 
{
    // TODO Auto-generated method stub
    ArrayList<NameValuePair> pair=new ArrayList<NameValuePair>();
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(ip);
        httpPost.setEntity(new UrlEncodedFormEntity(pair));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }
    catch (Exception e) {
        Log.e("Webservice 1", e.toString());
    }
    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();

        while((line = reader.readLine()) != null) {

            sb.append(line + "\n");
            Log.v("item", line);
        }

        is.close();
        result = sb.toString();         
    }
    catch (Exception e) {
        Log.e("Webservice 2", e.toString());
    }
    try {

        JSONArray ja = new JSONArray(result);
        JSONObject jo = null;

        list = new ArrayList<String>();

        for(int i=0; i<ja.length(); i++) {

            jo = ja.getJSONObject(i);
            list.add(jo.getString("name"));
        }           
    }catch (Exception e) {
        Log.e("Webservice 3", e.toString());
    }
}   

public void setData(){
    ArrayAdapter<String> adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);       
    spin.setAdapter(adp);
}

}

  

这是错误

06-17 07:22:44.301: E/Trace(2304): error opening trace file: No such file or directory (2)
06-17 07:22:44.811: D/libEGL(2304): loaded /system/lib/egl/libEGL_emulation.so
06-17 07:22:44.821: D/(2304): HostConnection::get() New Host Connection established 0x2a154fa0, tid 2304
06-17 07:22:44.841: D/libEGL(2304): loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-17 07:22:44.851: D/libEGL(2304): loaded /system/lib/egl/libGLESv2_emulation.so
06-17 07:22:44.891: E/Webservice 1(2304):                                   org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
06-17 07:22:44.891: E/Webservice 2(2304): java.lang.NullPointerException: lock == null
06-17 07:22:44.911: W/EGL_emulation(2304): eglSurfaceAttrib not implemented
06-17 07:22:44.921: E/Webservice 3(2304): java.lang.NullPointerException
06-17 07:22:44.930: D/OpenGLRenderer(2304): Enabling debug mode 0
06-17 07:22:45.291: V/error(2304): java.lang.NullPointerException
06-17 07:24:53.270: D/dalvikvm(2304): GC_CONCURRENT freed 165K, 11% free 2607K/2912K,  paused 73ms+3ms, total 138ms
06-17 07:24:53.441: W/EGL_emulation(2304): eglSurfaceAttrib not implemented

0 个答案:

没有答案