【问题标题】:Can't connect to google cloud endpoint from android device无法从安卓设备连接到谷歌云端点
【发布时间】:2016-03-08 18:35:31
【问题描述】:

我正在学习使用谷歌云端点。我正在尝试从我的设备连接,但是我不断收到错误消息。

在 20000 毫秒后无法连接到 /192.168.1.100(端口 8080):isConnected 失败:ECONNREFUSED(连接被拒绝)

如何解决这个问题,我想在真机上运行?

获取 Jokes.class

public class GetJokes extends AsyncTask<Pair<Context, String>, Void, String> {
private static MyApi myApiService = null;
private Context context;

@Override
protected String doInBackground(Pair<Context, String>... params) {
    if (myApiService == null) {  // Only do this once
        MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
            new AndroidJsonFactory(), null)
            // options for running against local devappserver
            // - 10.0.2.2 is localhost's IP address in Android emulator
            // - turn off compression when running against local devappserver
            .setRootUrl("http://10.0.2.2:8080/_ah/api/")
            .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                @Override
                public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                    abstractGoogleClientRequest.setDisableGZipContent(true);
                }
            });
        // end options for devappserver

        myApiService = builder.build();
    }

    context = params[0].first;
    String name = params[0].second;

    try {
        return myApiService.sayHi(name).execute().getData();
    } catch (IOException e) {
        return e.getMessage();
    }
}

@Override
protected void onPostExecute(String result) {
    Toast.makeText(context, result, Toast.LENGTH_LONG).show();
    Log.i("Failed to con", result);
}
}

MainActivityFragment.class

public class MainActivityFragment extends Fragment {

public static final String JOKE = "JOKE";

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_main, container, false);

    Button tellJokeButton = (Button)root.findViewById(R.id.tellJoke_button);
    tellJokeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new GetJokes().execute(new Pair<Context, String>(getActivity(), "Manfred"));
        }
    });

    AdView mAdView = (AdView) root.findViewById(R.id.adView);
    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device. e.g.
    // "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
    mAdView.loadAd(adRequest);
    return root;
}

public void tellJoke(){

    Intent intent = new Intent(getActivity(), JokeTellerActivity.class);
    //TODO: get jokes from Google Cloud endpoint
    intent.putExtra(JOKE,

        getActivity().getString(R.string.joke)
    );

    startActivity(intent);
}
}

MyEndPoint.class

public class MyEndpoint {

public static List<Joke> mJokes = new ArrayList<>();

/** A simple endpoint method that takes a name and says Hi back */
@ApiMethod(name = "sayHi")
public MyBean sayHi(@Named("name") String name) {
    MyBean response = new MyBean();
    response.setData("Hi, " + name);

    return response;
}
}

我尝试在几个不同的设备上运行它,但没有一个可以连接。

【问题讨论】:

    标签: android google-cloud-endpoints


    【解决方案1】:

    我也遇到了同样的问题,因为我也在 Android Nanodegree (Udacity) 做这个项目。

    我在this question 中找到了解决方案。因为那里的答案有些混乱或不完整,所以我将详细解释我在计算机上运行本地服务器并使用 真实 设备对其进行测试的操作。

    1) 将httpAddress = "0.0.0.0" 添加到后端(Google Cloud Endpoint)模块的build.gradle 文件中的appengine 块中,如下所示:

    appengine {
        ...
        httpAddress = "0.0.0.0"
    }
    

    根据对链接问题的评论,这意味着“将从任何地方接受”。

    2) 在本地运行您的后端模块(即启动 Web 服务器)。这很简单:在下拉菜单中选择后端配置,然后按绿色按钮。你可以find more detailed instructions here

    您现在应该可以打开浏览器(在您的计算机上)并导航到 http://localhost:8080/ 并看到一个带有“Hello,Endpoints!”的网页和许多其他的东西。

    3) 找到您计算机的 IP 地址(在 Mac 上转到系统偏好设置 -> 网络),然后在您的 Android 应用代码中将其设置为根 url,如下所示:

    MyApi.Builder builder = new MyApi.Builder(
                                AndroidHttp.newCompatibleTransport(),
                                new AndroidJsonFactory(), 
                                null
    ).setRootUrl("http://192.168.1.36:8080/_ah/api/")
    

    就是这样!

    【讨论】:

      【解决方案2】:

      所以您已经在运行 Android 模拟器的同一台机器上设置了一个基于 AppEngine 的云端点作为本地开发服务器?确保您为端点使用正确的 IP 地址(和端口)。您需要的本地 IP 地址不是 Android 模拟器正在使用的地址,而是主机开发机器。例如:

      本地开发机器:192.168.1.100

      开发机器上的 AppEngine 服务器:监听 8080

      在开发机器上运行的 Android 模拟器:10.0.2.2

      您也应该能够通过 Android 模拟器上的浏览器访问开发机器上的 API 资源管理器。您可以使用它来检查您的应用是否使用了正确的地址:

      http://192.168.1.100:8080/_ah/api/explorer
      

      【讨论】:

      • 我尝试了一些与我在研究中发现的不同的本地开发机器,但似乎都不起作用。但是,在 google 示例中有一条评论说“在针对本地 devappserver 运行时关闭压缩”,我该怎么做呢?
      • 构建器上应该有一个API方法,或者如果你得到底层HttpRequest或类似的禁用压缩。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 2019-04-01
      • 2020-08-03
      相关资源
      最近更新 更多