2016-12-17 8 views
0

サーバーとの接続を作成して使用するために1.9を使用していますが、これは完全にアプリケーションで動作しますが、サービスでapiをヒットしようとすると、このエラーを返しています一部のデバイスのサービス内でREST APIに接続できません

"retrofit.RetrofitError:Connection {、proxy = DIRECT @ cipherSuite = none protocol = http/1.1}(リサイクルカウント= 0)で予期しないストリームの終了" 私の小米科技Redmiノートのインスタンス3.

ここでは私のコードです: -

public class TrackingService extends Service implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    LocationListener { 
private String latitude = "", longitude = ""; 
private PrefsManager mPrefs; 
int requestID = 42; 
private int time = 120000; 
private static final long SERVICE_INTERVAL = 60000; 
private static final long SERVICE_FASTEST = 30000; 
private static final long ONE_MIN = 1000; 
private GoogleApiClient googleApiClient; 
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi; 
private boolean canGetLocation = false; 
private LocationRequest locationRequest; 
private LocationManager locationManager; 
private final String TAG = "MyAwesomeApp"; 
private Runnable runnable = new Runnable() { 
    @Override 
    public void run() { 
     updateLocation(); 
    } 
}; 
private Handler handler = new Handler(new Handler.Callback() { 
    @Override 
    public boolean handleMessage(Message msg) { 
     getHandler1().postDelayed(runnable, time); 
     return true; 
    } 
}); 
private Handler handler1 = new Handler(); 

public Handler getHandler1() { 
    return handler1; 
} 

private NotificationCompat.Builder mBuilder; 
private BroadcastReceiver receiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     canGetLocation = !(!locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER)); 
    } 
}; 
private BroadcastReceiver receiverNetwork = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     handler.removeCallbacksAndMessages(null); 
     getHandler1().removeCallbacks(runnable); 
     time = 5000; 
     handler.sendMessage(new Message()); 
    } 
}; 
private static API REST_CLIENT; 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    registerReceiver(receiverNetwork, new IntentFilter(Constants.NETWORK_BROADCAST)); 
    registerReceiver(receiver, new IntentFilter(Constants.INTENT_LOCATION_SERVICE)); 
} 

private void init() { 
    mPrefs = new PrefsManager(this); 
    locationRequest = LocationRequest.create(); 
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
    locationRequest.setInterval(SERVICE_INTERVAL); 
    locationRequest.setFastestInterval(SERVICE_FASTEST); 
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    canGetLocation = !(!locationManager 
      .isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager 
      .isProviderEnabled(LocationManager.NETWORK_PROVIDER)); 
    googleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API).addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this).build(); 
    try { 
     googleApiClient.connect(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    updateLocation(); 
} 

@Override 
public void onDestroy() { 
    Log.e(TAG, "GPSTrackerNew destroyed!"); 
    googleApiClient.disconnect(); 
    try { 
     unregisterReceiver(receiverNetwork); 
     unregisterReceiver(receiver); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    this.stopForeground(true); 
    super.onDestroy(); 
} 

/** 
* Update driver location for live tracking 
*/ 
private void updateLocation() { 
    if (canGetLocation()) { 
     /*setNotification(this, getResources().getString(R.string.tracking));*/ 
     if (!TextUtils.isEmpty(latitude) && !TextUtils.isEmpty(longitude)) { 
      get().updateLocation(mPrefs.getAccessToken(), latitude, longitude, 
        new Callback<PojoBase>() { 
         @Override 
         public void success(PojoBase pojoBase, Response response) { 
          try { 
           if (pojoBase.status == Constants.SUCCESS) { 
            time = 120000; 
            handler.removeCallbacksAndMessages(null); 
            getHandler1().removeCallbacks(runnable); 
            handler.sendMessage(new Message()); 
            setNotification(TrackingService.this, getResources().getString(R.string.tracking)); 
           } else if (pojoBase.status == Constants.LOGIN_EXPIRED) { 
            onDestroy(); 
           } 
          } catch (Exception e) { 
           onDestroy(); 
           e.printStackTrace(); 
          } 
         } 

         @Override 
         public void failure(RetrofitError error) { 
          time = 5000; 
          setNotification(TrackingService.this, getResources().getString(R.string.network_error)); 
          handler.removeCallbacksAndMessages(null); 
          getHandler1().removeCallbacks(runnable); 
          handler.sendMessage(new Message()); 
         } 
        }); 
     } else { 
      time = 5000; 
      handler.removeCallbacksAndMessages(null); 
      getHandler1().removeCallbacks(runnable); 
      handler.sendMessage(new Message()); 
     } 
    } else { 
     time = 5000; 
     handler.removeCallbacksAndMessages(null); 
     getHandler1().removeCallbacks(runnable); 
     handler.sendMessage(new Message()); 
     setNotification(this, getResources().getString(R.string.not_able_to_fetch_location)); 
    } 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    init(); 
    return START_STICKY; 
} 

private void setNotification(Context context, String message) { 
    Intent intent = null; 
    intent = new Intent(this, HomeActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, requestID, 
      intent, 0); 
    mBuilder = new NotificationCompat.Builder(context) 
      .setContentTitle(getResources().getString(R.string.app_name)) 
      .setOngoing(true) 
      .setContentText(message) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(pendingIntent); 
    this.startForeground(requestID, mBuilder.build()); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    fusedLocationProviderApi.requestLocationUpdates(googleApiClient, 
      locationRequest, this); 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.i(TAG, "GoogleApiClient connection has been suspend"); 
} 

@Override 
public void onLocationChanged(Location location) { 
    if (location != null) { 
     Log.i("From service:", "Location received: " + location.toString()); 
     canGetLocation = true; 
     latitude = String.valueOf(location.getLatitude()); 
     longitude = String.valueOf(location.getLongitude()); 
    } else { 
     canGetLocation = false; 
    } 


} 

public boolean canGetLocation() { 
    return canGetLocation; 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Log.i(TAG, "GoogleApiClient connection has been failed"); 
} 

public static API get() { 
    if (REST_CLIENT == null) { 
     RestAdapter.Builder adapter = new RestAdapter.Builder() 
       .setClient(new OkClient(getClient())); 
     adapter.setLogLevel(RestAdapter.LogLevel.BASIC); 
     adapter.setEndpoint(Constants.ROOT); 
     RestAdapter mAdapter = adapter.build(); 
     REST_CLIENT = mAdapter.create(API.class); 
    } 
    return REST_CLIENT; 
} 

private static OkHttpClient getClient() { 
    OkHttpClient client = new OkHttpClient(); 
    client.setConnectTimeout(30, TimeUnit.SECONDS); 
    return client; 
} 

}

なぜこのようなことが起こっているのでしょうか?

TIA

+0

サーバー側のエラーです。サーバーは、restrofitによって解析されない応答を返します。ログで確認し、.BASICから.BODYにログレベルを更新してみてください。そして何がうまくいかないのかを調べる – USKMobility

答えて

0

は、代わりにエンキューcall.Servicesを実行使用してみてください別のスレッドですでにあります。したがって、別のスレッドでネットワークコールを行っている場合は、同じスレッドでapiを実行する必要があります。

関連する問題