我使用QP Express api在Android中收集从一个地方到另一个地方的航班数据。当我运行应用程序时,请求不会发送到谷歌。我会错过什么?我在清单中有客户授权,如下所示。我做什么不对?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.steve.myflights" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<meta-data
android:name="com.google.android.apps.drive.APP_ID" />/clients key
android:value="*****(my client api key)**" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
下面是MainActivity.java,以防对有用
package com.example.steve.myflights;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import java.io.IOException;
import java.util.*;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.qpxExpress.QPXExpressRequestInitializer;
import com.google.api.services.qpxExpress.QPXExpress;
import com.google.api.services.qpxExpress.model.FlightInfo;
import com.google.api.services.qpxExpress.model.LegInfo;
import com.google.api.services.qpxExpress.model.PassengerCounts;
import com.google.api.services.qpxExpress.model.PricingInfo;
import com.google.api.services.qpxExpress.model.SegmentInfo;
import com.google.api.services.qpxExpress.model.SliceInfo;
import com.google.api.services.qpxExpress.model.TripOption;
import com.google.api.services.qpxExpress.model.TripOptionsRequest;
import com.google.api.services.qpxExpress.model.TripsSearchRequest;
import com.google.api.services.qpxExpress.model.SliceInput;
import com.google.api.services.qpxExpress.model.TripsSearchResponse;
public class MainActivity extends AppCompatActivity {
private EditText editTextOrigin;
private EditText editTextArrival;
private EditText editTextDestination;
private EditText editTextFlightNum;
private EditText editTextPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String APPLICATION_NAME = "com.example.MyFlightsApp";
final String API_KEY = "***APIKEY****";
/** Global instance of the HTTP transport. */
HttpTransport httpTransport;
/** Global instance of the JSON factory. */
final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
PassengerCounts passengers= new PassengerCounts();
passengers.setAdultCount(1);
List<SliceInput> slices = new ArrayList<SliceInput>();
SliceInput slice = new SliceInput();
slice.setOrigin("NYC");
slice.setDestination("JFK");
slice.setDate("2015-11-11");
slices.add(slice);
TripOptionsRequest request= new TripOptionsRequest();
request.setSolutions(1);
request.setPassengers(passengers);
request.setSlice(slices);
TripsSearchRequest parameters = new TripsSearchRequest();
parameters.setRequest(request);
QPXExpress qpXExpress= new QPXExpress.Builder(httpTransport, JSON_FACTORY, null).setApplicationName(APPLICATION_NAME).setGoogleClientRequestInitializer(new QPXExpressRequestInitializer(API_KEY)).build();
TripsSearchResponse list= qpXExpress.trips().search(parameters).execute();
List<TripOption> tripResults=list.getTrips().getTripOption();
String id;
editTextArrival = (EditText)findViewById(R.id.ediTextArrival);
editTextOrigin = (EditText)findViewById(R.id.ediTextOrigin);
editTextDestination = (EditText)findViewById(R.id.ediTextDestination);
editTextFlightNum = (EditText)findViewById(R.id.ediTextFlightNum);
editTextPrice = (EditText)findViewById(R.id.ediTextPrice);
for(int i=0; i<tripResults.size(); i++) {
//Trip Option ID
id= tripResults.get(i).getId();
//Slice
List<SliceInfo> sliceInfo= tripResults.get(i).getSlice();
for(int j=0; j<sliceInfo.size(); j++) {
int duration= sliceInfo.get(j).getDuration();
List<SegmentInfo> segInfo= sliceInfo.get(j).getSegment();
for(int k=0; k<segInfo.size(); k++) {
FlightInfo flightInfo=segInfo.get(k).getFlight();
editTextFlightNum.setText(flightInfo.getNumber());
List<LegInfo> leg=segInfo.get(k).getLeg();
for(int l=0; l<leg.size(); l++) {
editTextArrival.setText(leg.get(1).getAircraft());
editTextDestination.setText(leg.get(1).getDestination());
editTextOrigin.setText(leg.get(1).getOrigin());
}
}
}
//Pricing
List<PricingInfo> priceInfo= tripResults.get(i).getPricing();
for(int p=0; p<priceInfo.size(); p++) {
editTextPrice.setText(priceInfo.get(p).getSaleTotal());
}
}
return;
} catch (IOException e) {
System.err.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
// System.exit(1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
您应该在清单文件中添加访问网络的权限并了解网络。