2012-06-17 3 views
0

비동기 작업을 사용하여 PHP 서버에서 데이터를 가져 오기 위해 HttpPost를 수행하고 있습니다. 기본적으로 PHP 스크립트는 JSON 배열 또는 null을 반환합니다. json 배열 반환 될 때 잘 작동하지만 스크립트가 null을 반환하는 경우 내 if 문이 선택되지 않고이 오류가 반환됩니다.startActivity() from insidePostExecute() not working

데이터를 구문 분석하는 동안 오류가 발생했습니다. org.json.JSONException : type null

@Override 
     protected Void doInBackground(String... params) { 
      String url_select = "http://localhost/test.php"; 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url_select); 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
     nameValuePairs.add(new BasicNameValuePair("id", id)); 
      try { 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       //read content 
       is = httpEntity.getContent(); 

      } catch (Exception e) { 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
       } 

      try { 
       BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 
       String line = ""; 
        while((line=br.readLine())!=null){ 
         sb.append(line+"\n"); 
        } 
       is.close(); 
       result=sb.toString(); 
      } catch (Exception e) { 
       Log.e("log_tag", "Error converting result "+e.toString()); 
      } 
      return null; 

     } 

     protected void onPostExecute(Void v) { 

     if(result == "null"){ 
     this.progressDialog.dismiss(); 
      startActivity(new Intent(viewRandom.this, allDone.class)); 
     }else{ 

     try { 
      JSONArray Jarray = new JSONArray(result); 
      for(int i=0;i<Jarray.length();i++){ 
       JSONObject Jasonobject = null; 
       Jasonobject = Jarray.getJSONObject(i); 
       String id = Jasonobject.getString("id"); 
     } 
      this.progressDialog.dismiss(); 

     } catch (Exception e) { 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 
     } 
} 
+0

변경과 같은 onPostExecute 뭔가이 경우 (결과 == "널 (null)")을 정수 값을 반환하도록해야하는 경우 (result.equals ("널 (null)")) . – Akram

+0

오류가 발생한 줄을 알려주십시오. – Sunny

답변

2

변경 if(result == null)-if(result == "null") : org.json.JSONObject $ 1 JSONArray

로 변환 할 수없는이 내 스크립트의 조각이다.

당신이 문자열 "null"를 확인하려면

.equals() 함께 할 : if ("null".equals(result))

난 당신이 정말 다시 서버에서 그러나 어쨌든 "NULL"문자열을 전송하는 경우 확실하지 않다. null (문자열이 아님!)을 반환 할 수도 있으므로 그 점도 확인해야합니다.

편집 : 왜 "null".equals(result)result.equals("null")보다 우수합니까? 그 대답은 다음과 같습니다. 첫 번째 것은 null-safe이며 이는 result이 null 일 때 NullPointerException을 발생시키지 않음을 의미합니다. 두 번째 경우에는 예외가 발생합니다. 대신 반환

+0

나는이 시도하고 logcat 여전히 06-17 10 : 38 : 20.173 : E/log_tag (29836) : 데이터 분석 오류 org.json.JSONException : org.json.JSONObject $ 1 JSONArray 변환 할 수없는 값을 null을 던지고있다 –

+0

질문에있는 코드를 현재 코드로 업데이트 해 주시겠습니까? else 문에 들어가기 때문에 if 절에 여전히 오류가있는 것 같습니다. – WarrenFaith

+0

실제로 이것은 잘 작동했습니다. 내 생각에 그것은 나쁜 생각이 내 PHP 스크립트 값을 "null"을 스팅으로 반환하는 것 같아요. 스크립트를 변경하여 아무것도 반환하지 않고 if 문을 다음과 같이 설정합니다. "".equals (result) 그리고 완벽하게 작동합니다. 고맙습니다. –

0

널 여러분이

@Override 
public Integer doInBackground(String...params){ 
    ....... 
    ....... 
    return 1; 
} 


protected void onPostExecute(Integer v) { 
    if(v==1) { 
    } 
}