連接到谷歌雲端點我學習與谷歌的雲終端工作。我試圖從我的設備連接,但是,我不斷收到錯誤。無法從Android設備
無法連接到/192.168.1.100 20000ms後(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("ABCDEF") 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;
}
}
我試過幾個不同的設備上運行它,沒有人會連接。
我嘗試了幾種不同的本地開發機器,從我在研究中發現的所有東西看起來都不起作用。然而,在谷歌的例子中有一個評論,說「關閉本地devappserver運行時壓縮」,我該怎麼做呢? – Sam321pbs
在構建器上應該有一個API方法,或者如果您獲得底層的'HttpRequest'或類似的命令來禁用壓縮。 –