gpt4 book ai didi

android - 无法从 Android 设备连接到谷歌云端点

转载 作者:行者123 更新时间:2023-11-30 01:30:01 24 4
gpt4 key购买 nike

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

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 Nanodegree (Udacity) 的项目。

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

1) 将 httpAddress = "0.0.0.0" 添加到后端 build.gradle 文件中的 appengine block (谷歌云端点)模块,像这样:

appengine {
...
httpAddress = "0.0.0.0"
}

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

2) 在本地运行后端模块(即启动网络服务器)。这很简单:在下拉菜单中选择后端配置,然后按绿色按钮。你可以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/")

就是这样!

关于android - 无法从 Android 设备连接到谷歌云端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35875281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com