2014-12-23 7 views
2

InyncTask android. 다른 클래스의 메소드를 호출 할 때 doInBackGround() 태스크에서 null과 같은 예외가 발생합니다. Android AsyncTask 예외가 null이 됨

심지어 하드 내가 가진 예외 전자입니다 (URL이 방법은, JSON은),

 protected JSONArray doInBackground(Void... arg0) { 

      try { 
       return rest.request(url, method, json); // <-- returns json array 
      } catch (Exception e) { 
       this.e = e; 
      } 
      return null; // <--- returning this null 

     } 

다른 일들이처럼

private class doRequest extends AsyncTask<Void, JSONArray, JSONArray> 
protected void onPostExecute(JSONArray data) 


/*rest client class*/ 
public class AndrestClient { 

// The client to use for requests 
DefaultHttpClient client = new DefaultHttpClient(); 
public JSONArray request(String url, String method, String json) throws RESTException { 
     if (method.matches("GET")) { 
      return get(url); 
     } else if (method.matches("POST")) { 
      return post(url, json); 
     } else if (method.matches("PUT")) { 
      //return put(url, data); 
     } else if (method.matches("DELETE")) { 
      //return delete(url); 
     } 
     throw new RESTException("Error! Incorrect method provided: " + method); 
    } 

    public JSONArray get(String url) throws RESTException { 

     String jsonjr = "['Chanuthi','Damsith','Dunili','Isath','Minuka','Uvin','Vidath']"; 

     JSONArray jsonAraay = null; 
     try { 
      jsonAraay = new JSONArray(jsonjr); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return jsonAraay; 
} 
} 

작동하지 않는 rest.request 내부 코드 = null. 다른 모든 것들이 제대로 작동합니다. doInBackGround에서 결과를 하드 코딩 할 때 제대로 작동합니다. 또한 나머지 클라이언트 get 메서드는 정확한 것을 반환합니다.

+3

우리가 당신을 도울 수 있도록 더 많은 코드를 제공하십시오. 또한 예외의 스택 추적을 게시하고 정확하게 어떤 행이 올지를 게시하십시오. –

+0

'나머지'의 요청 메소드는 어디에 정의되어 있습니까? 또한, url, method 및 json은 어디에 정의되어 있습니까? – MiiinimalLogic

+0

사실, 질문 텍스트와 코드 주석은 모순입니다. null의 예외를 얻는다면 코드 주석처럼 자연스럽게 null을 반환 할 수 없습니다. 그래서 어떤가? – MiiinimalLogic

답변

1

AsyncTask가 부적절하게 사용되는 것 같습니다. 첫째, 당신 해야 서브 클래스/둥지 안드로이드 문서에 따라 당신의 AsyncTask를 :

http://developer.android.com/reference/android/os/AsyncTask.html

또한, 중첩 된 클래스에서 outter는 클래스의 메서드를 호출하기위한 기본 규칙을 따라야합니다. 무언가를 수행하여 메소드를 호출, 매개 변수로 doInBackground에 AndrestClient 객체를 전달하는 AsyncTask를

  • 의 onPreExecute에서 AndrestClient 객체를() 만들기

    • :

      처럼, 몇 가지 대안이있다 outter 클래스에서 이와 같이 :

      doRequest.execute (rest);