2017-02-14 73 views
0

我有一個類的Android HttpURLConnection類總是拋出異常

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.util.ArrayList; 
import java.lang.Exception; 
import java.net.HttpURLConnection; 
public class HttpURLConnectionExample { 
    private final String USER_AGENT = "Mozilla 5.0"; 
    // HTTP GET request 
    public ArrayList<dispencer> sendGet() throws Exception { 
     String url = "http://192.168.1.9"; 
     URL obj = new URL(url); 
     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
     // optional default is GET 
     con.setRequestMethod("GET"); 
     //add request header 
     con.setRequestProperty("User-Agent", USER_AGENT); 
     int responseCode = con.getResponseCode(); 
     System.out.println("\nSending 'GET' request to URL : " + url); 
     System.out.println("Response Code : " + responseCode); 
     BufferedReader in = new BufferedReader(
       new InputStreamReader(con.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 
     while ((inputLine = in.readLine()) != null) { 
     response.append(inputLine); 
    } 
    in.close(); 
    //print result 
    ArrayList<dispencer> dispList = new ArrayList<dispencer>(); 
    String[] strRows = response.toString().split("@"); 
    for (int i= 0; i <strRows.length; i++) { 
     dispencer disp = new dispencer(strRows[i]); 
     if (!disp.isOk) continue; 
     dispList.add(disp); 
     System.out.println(disp.indirizzo); 
    } 
    return dispList; 
} 
} 

我調用該方法sendGet這樣。

 HttpURLConnectionExample httpFnd= new HttpURLConnectionExample(); 
      ArrayList<dispencer> listAr = new ArrayList<dispencer>(); 
      try{ 
       listAr = httpFnd.sendGet(); 
      } 
      catch (Exception Exception){ 

       fndLayout.setVisibility(View.GONE); 
       System.out.println("exception handled"); 
      } 

每當我試着打電話sendGet我總是得到處理的消息例外,從來沒有收到我的arrayList回來,就像是從來沒有建立的連接。 當我調用該方法我也收到此錯誤

d/NetworkSecurityConfig:沒有網絡安全配置的規定,使用 平臺默認

,難道是我的問題的原因是什麼?

感謝您的幫助。

編輯我看到我需要使用asyncTask來預防拋出異常安迪關於我如何實現它的建議?

+0

添加完整的錯誤日誌 – Jayanth

+0

什麼例外是trhown ? –

+0

這是錯誤日誌02/14 18:03:54:啓動應用程序 W/System:ClassLoader引用未知路徑:/data/data/com.example.lele.condomadv/lib 熱交換更改,活動重新啓動 E/EGL_emulation:TID 4615:eglSurfaceAttrib(1174):錯誤0x3009(EGL_BAD_MATCH) W/OpenGLRenderer:無法設置EGL_SWAP_BEHAVIOR上表面0x9a6e0060,誤差= EGL_BAD_MATCH E/EGL_emulation:TID 4615:eglSurfaceAttrib(1174):錯誤0x3009(EGL_BAD_MATCH) W/OpenGLRenderer:無法設置表面上的EGL_SWAP_BEHAVIOR 0x9aac73e0,錯誤= EGL_BAD_MATCH –

回答

0

試試這個:

class Loading extends AsyncTask {

@Override

protected Void doInBackground(Void... arg0)

{

// your code here

return null;

}

@Override

protected void onPreExecute()

{

//your code here

super.onPreExecute();

}

@Override

protected void onPostExecute(Void result)

{

//your code here

super.onPostExecute(result);

}

}//Loading

你記得添加在AndroidManifest.xml這一行:

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

+0

謝謝我會嘗試,關於權限是我給了這些權限;) –