2015-04-17 171 views
0

我寫一個使用互聯網的應用程序,我已經給了以下權限:Android的互聯網連接問題

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.NETWORK"/> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

一切都很好WIFI的時候,但是當它連接到H + 3G或4G點處停下工作POST或GET。我已經把所有請求或發佈到服務器的任務放在aSyncTask中,但沒有任何結果。

有什麼我失蹤?

編輯: 與代碼活動

public class ParkeerActivity extends ActionBarActivity { 


    @ 
    Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_parkeer); 

    DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(new Date()); 
    datePicker.init(calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR), new DatePicker.OnDateChangedListener() {@ 
     Override 
     public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
     updateSpacesLeft(); 
     } 
    }); 
    RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup); 
    rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {@ 
     Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
     updateSpacesLeft(); 
     } 
    }); 
    Button reservationBtn = (Button) findViewById(R.id.reservationButton); 
    reservationBtn.setEnabled(false); 
    reservationBtn.setOnClickListener(new View.OnClickListener() {@ 
     Override 
     public void onClick(View v) { 
     reservationClicked(); 

     } 
    }); 
    } 


    @ 
    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_parkeer, 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); 
    } 
    public void reservationClicked() { 
    AsyncTask < Void, Void, String > task = new AsyncTask < Void, Void, String >() {@ 
     Override 
     protected String doInBackground(Void...params) { 
     String formattedDate = getFormattedDateForUrl(); 
     String selectedTime = getSelectedTime(); 
     EditText commentText = (EditText) findViewById(R.id.editText); 
     String comment = commentText.getText().toString(); 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ParkeerActivity.this); 
     String accessToken = prefs.getString("accessToken", null); 
     Ion.with(ParkeerActivity.this) 
      .load("http://-/reservations") 
      .setBodyParameter("accessToken", accessToken) 
      .setBodyParameter("date", formattedDate) 
      .setBodyParameter("time", selectedTime) 
      .setBodyParameter("comment", comment) 
      .asJsonObject() 
      .withResponse() 
      .setCallback(new FutureCallback < Response <JsonObject>>() {@ 
      Override 
      public void onCompleted(Exception e, Response <JsonObject> result) { 
       if (result.getHeaders().code() == 201) { 
       AlertDialog.Builder alert = new AlertDialog.Builder(ParkeerActivity.this); 
       alert.setMessage("Reservering gelukt"); 
       alert.show(); 
       } else if (result.getHeaders().code() != 201) { 
       AlertDialog.Builder alert = new AlertDialog.Builder(ParkeerActivity.this); 
       alert.setMessage("Reservering mislukt"); 
       alert.show(); 
       }; 
      } 
      }); 
     return null; 
     } 
    }; 
    task.execute(); 
    } 

崩潰在reservationClicked()

logcat的一次,我已經找到了光纜到我的手機連接到PC將被張貼

EDIT2:移動設備上的Logcat異常(不是仿真器)

04-18 13:48:48.489 18475-19199/com.parkeerapp.-.parkeerapp D/MyLogs﹕ (296 ms) http://-/parkingspaces/available: Connecting socket 
04-18 13:48:48.489 18475-19199/com.parkeerapp.-.parkeerapp D/MyLogs﹕ (0 ms) http://-/parkingspaces/available: Executing request. 
04-18 13:48:48.499 18475-19199/com.parkeerapp.-.parkeerapp D/MyLogs﹕ (2 ms) http://-/parkingspaces/available: Connecting socket 
04-18 13:48:58.549 18475-19199/com.parkeerapp.-.parkeerapp D/MyLogs﹕ (10357 ms) http://-/parkingspaces/available: Response is not cacheable 
04-18 13:48:58.559 18475-19199/com.parkeerapp.-.parkeerapp D/MyLogs﹕ (10359 ms) http://-/parkingspaces/available: Connection successful 
04-18 13:48:58.569 18475-19199/com.parkeerapp.-.parkeerapp D/MyLogs﹕ (10078 ms) http://-/parkingspaces/available: Response is not cacheable 
04-18 13:48:58.569 18475-19199/com.parkeerapp.-.parkeerapp D/MyLogs﹕ (10079 ms) http://-/parkingspaces/available: Connection successful 
04-18 13:48:58.579 18475-18475/com.parkeerapp.-.parkeerapp D/AndroidRuntime﹕ Shutting down VM 
04-18 13:48:58.579 18475-18475/com.parkeerapp.-.parkeerapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41859da0) 
04-18 13:48:58.579 18475-19199/com.parkeerapp.-.parkeerapp D/MyLogs﹕ (10386 ms) http://-/parkingspaces/available: Recycling keep-alive socket 
04-18 13:48:58.579 18475-18475/com.parkeerapp.indivirtual.parkeerapp E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.parkeerapp.-.parkeerapp, PID: 18475 
    java.lang.NullPointerException 
      at com.parkeerapp.-.parkeerapp.ParkeerActivity$5$1.onCompleted(ParkeerActivity.java:154) 
      at com.parkeerapp.-.parkeerapp.ParkeerActivity$5$1.onCompleted(ParkeerActivity.java:151) 
      at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:107) 
      at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:141) 
      at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:124) 
      at com.koushikdutta.ion.IonRequestBuilder$1.run(IonRequestBuilder.java:244) 
      at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(AsyncServer.java:57) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:146) 
      at android.app.ActivityThread.main(ActivityThread.java:5679) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
      at dalvik.system.NativeStart.main(Native Method) 
04-18 13:48:58.589 18475-19199/com.parkeerapp.-.parkeerapp D/MyLogs﹕ (10094 ms) http://-/parkingspaces/available: Recycling keep-alive socket 
+7

您錯過了查看LogCat的部分,看看它爲什麼崩潰。 – immibis

+0

你可以請求發佈一些代碼嗎? –

+0

可以看出你的問題是空的,而不是連接。更好地記錄創建類中的每一步,例如datePicker,calender,rg和其他meybe null。如果可能的話通過調試追蹤,你會發現代碼中斷的地方。它可以幫助你。 –

回答

0

問題一直存在使用上3G和4G代理

Ion.with(ParkeerActivity.this) 
        .load(url) 
        .proxy(ip_adres, 8080) 

的伎倆的

目前的問題是谷歌的Oauth說需要的權限,但將是一個新的線程

0
  1. 如果以這種方式使用Ion,則不需要使用AsyncTask。
  2. 您應該先檢查resultnull,然後再盲目前往。

我有時會遇到類似的問題。如果連接中斷,它有時會返回result作爲null

@Override 
public void onCompleted(Exception e, JsonObject result) { 
    if (e != null) { 
     // failed. 
     // return 
    } 

    if (result == null) { 
     // failed 
     // return 
    } 
} 

有時即使異常爲空,結果也爲空。