如何将我的Android模拟器连接到我的本地Web服务器

时间:2013-08-24 14:00:44

标签: android android-emulator localhost

我使用xampp在自己的计算机上设置了一个Web服务器,我试图在我的应用程序中访问服务器,如下所示:

package com.example.connect2php;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

公共类MainActivity扩展了Activity {

static String yahooStackInfo = "http://localhost/21/test03.php";

static String stockSymbol = "";
static String stockDaysLow = "";
static String stockDaysHigh = "";
static String stockChange = "";

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

    new MyAsyncTask().execute();

}

private class MyAsyncTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... parms) {
        // TODO Auto-generated method stub

        DefaultHttpClient httpclient = new DefaultHttpClient(
                new BasicHttpParams());

        HttpPost httppost = new HttpPost(yahooStackInfo);

        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;

        String result = null;

        try {

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder theStringBuilder = new StringBuilder();

            String line = null;

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

                theStringBuilder.append(line + "\n");

            }

            result = theStringBuilder.toString();

        }

        catch (Exception e) {

            e.printStackTrace();

        }

        finally {

            try {

                if (inputStream != null)
                    inputStream.close();

            }

            catch (Exception e) {

                e.printStackTrace();

            }

        }

        JSONObject jsonObject;

        try {

            result = result.substring(7);
            result = result.substring(0, result.length() - 2);

            // Log.v("JSONParser RESULT " , result);

            jsonObject = new JSONObject(result);

            JSONObject queryJSONObject = jsonObject.getJSONObject("query");

            JSONObject resultsJSONObject = queryJSONObject
                    .getJSONObject("results");

            JSONObject quoteJSONObject = resultsJSONObject
                    .getJSONObject("quote");

            stockSymbol = quoteJSONObject.getString("symbol");
            stockSymbol = quoteJSONObject.getString("DaysLow");
            stockSymbol = quoteJSONObject.getString("DaysHigh");
            stockSymbol = quoteJSONObject.getString("Change");

        }

        catch (JSONException e) {

            e.printStackTrace();

        }

        return result;
    }

    @Override
    protected void onPostExecute(String result) {

        TextView line1 = (TextView) findViewById(R.id.line1);
        TextView line2 = (TextView) findViewById(R.id.line2);
        TextView line3 = (TextView) findViewById(R.id.line3);

        line1.setText("Stack: " + stockSymbol + " : " + stockChange);
        line2.setText("Days Low " + stockDaysLow);
        line3.setText("Days High " + stockDaysHigh);

    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

但它没有用。服务器代码只是:

echo 'cbfunc({"query":{"count":1,"created":"2013-08-24T13:38:58Z","lang":"en-US","results":    {"quote":{"symbol":"MSFT","AverageDailyVolume":"47950000","Change":"+2.36","DaysLow":"34.00","DaysHigh":"35.20","YearLow":"26.26","YearHigh":"36.43","MarketCapitalization":"289.5B","LastTradePriceOnly":"34.75","DaysRange":"34.00 - 35.20","Name":"Microsoft Corpora","Symbol":"MSFT","Volume":"225493744","StockExchange":"NasdaqNM"}}}})';

这是ip详细信息:

@ppeterka 
Microsoft Windows [版本 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\hcleeab>ipconfig / all

錯誤: 無法辨識或不完整的命令列。

使用方式:
    ipconfig [/allcompartments] [/? | /all |
                             /renew [adapter] | /release [adapter] |
                             /renew6 [adapter] | /release6 [adapter] |
                             /flushdns | /displaydns | /registerdns |
                             /showclassid adapter |
                             /setclassid adapter [classid] |
                             /showclassid6 adapter |
                             /setclassid6 adapter [classid] ]

其中     adapter连线名称                        (允许使用万用字元*与?,请见范例)

選項:
   /?               顯示此說明訊息。
   /all             顯示完整設定資訊。
   /release         釋放指定介面卡的 IPv4 位址。
   /release6        釋放指定介面卡的 IPv6 位址。
   /renew           更新指定介面卡的 IPv4 位址。
   /renew6          更新指定介面卡的 IPv6 位址。
   /flushdns        清除 DNS 解析快取。
   /registerdns     重新整理所有 DHCP 租用並重新登錄 DNS 名稱。
   /displaydns      顯示 DNS 解析快取的內容。
   /showclassid     顯示介面卡所有允許的 DHCP 類別識別碼。
   /setclassid      修改 DHCP 類別識別碼。
   /showclassid6    顯示介面卡允許的所有 IPv6 DHCP 類別識別碼。
   /setclassid6     修改 IPv6 DHCP 類別識別碼。

预设是仅显示每个系结到TCP / IP之介面卡的IP位址,子网路遮罩及预设闸道。

对于释放与续约,如果没有指定介面卡名称,则会释放或更新所有系结到 TCP / IP介面卡的IP位址租用。

对于Setclassid与Setclassid6,如果没有指定ClassId,则将移除ClassId。

范例:

  

ipconfig ...显示资讯   ipconfig / all ...显示详细资讯   ipconfig / renew ...更新所有介面卡   ipconfig / renew EL * ...更新所有名称开头为EL的连线   ipconfig / release Con ...释放所有符合的连线,                                            例如“Local Area Connection 1”或                                                 “本地连接2”   ipconfig / allcompartments ...显示所有区间的相关资讯   ipconfig / allcompartments / all ...显示所有区间的详细资讯

C:\用户\ hcleeab&GT; IPCONFIG

Windows IP设置

无线区域网路介面卡无线网路连线:

媒体状态。 。 。 。 。 。 。 。 。 。 。 。 。:媒体已中断连线    连线特定DNS尾码。 。 。 。 。 。 。 。 :hgcbroadband.com

通道介面卡isatap.hgcbroadband.com:

媒體狀態 . . . . . . . . . . . . .: 媒體已中斷連線
連線特定 DNS 尾碼 . . . . . . . . : hgcbroadband.com

通道介面卡 6TO4 Adapter:


通道介面卡 Teredo Tunneling Pseudo-Interface:


預設閘道 . . . . . . . . . . . . .:

C:\Users\hcleeab>

2 个答案:

答案 0 :(得分:1)

您应该使用:

http://10.0.2.2/21/test03.php

10.0.2.2是主机环回接口的特殊别名(即开发机器上的127.0.0.1)

请参阅官方文档中的Emulator Network Address Space

答案 1 :(得分:0)

虽然您可以添加logcat,但这很可能是您的应用无法获取内容的原因:

static String yahooStackInfo = "http://localhost/21/test03.php";

这告诉你的Android设备尝试访问自己。 Localhost是设备本身。用您主机的主机名或IP地址替换它。

您可能还需要检查防火墙设置......

<强>更新

在评论中更新了IP:

static String yahooStackInfo = "http://223.18.60.177/21/test03.php";
相关问题