2013-06-22 2 views
0

바코드 스캐너 라이브러리를 사용하는 Android 앱이 있습니다. 스캐닝이 완료된 후 호출되는 Activity를 생성 할 때까지 모두 잘 작동합니다.Android 앱에서 JSON 배열을 구문 분석 할 수 없습니다.

해당 활동이 JSON 배열 사용

<?php 
$barCodes = array(
array(
    "id" => 123456, 
    "format" => "upc_1" 
), 
array(
    "id" => 39123439, 
    "format" => "upc_2" 
), 
array(
    "id" => 12345670, 
    "format" => "upc_3" 
) 
); 

에코로 json_encode ($ 바코드); ? >

이 JSONArray를 구문 분석하는 코드는 다음과 같습니다

String scanResult; 

    Intent intent = getIntent(); 
    scanResult = intent.getStringExtra("result"); 


HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://someAddress.php"); 


    try { 

    HttpResponse response = httpclient.execute(httppost); 
    String jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); 


    JSONObject object = new JSONObject(jsonResult); 

    JSONArray jArray = object.getJSONArray("barCodes"); 

    JSONObject json_data; 
    int id ; 
    String format ; 
    String ret_format[] = new String[jArray.length()]; 
    int ret_id[] = new int[jArray.length()]; 

    for(int i=0;i<jArray.length();i++){ 
     json_data = jArray.getJSONObject(i); 

     id = json_data.getInt("id"); 
     format = json_data.getString("format"); 

     ret_id[i] = id; 
     ret_format[i] = format; 
     } 


     int scanInt = Integer.parseInt(scanResult); 

     switch(scanInt) { 

     case 123456: 
      textView.setText(ret_id[0] + " - " + ret_format[0]); 
     break; 

     case 39123439: 
      textView.setText(ret_id[1] + " - " + ret_format[1]); 
     break; 

     case 12345670: 
      textView.setText(ret_id[2] + " - " + ret_format[2]); 
     break; 

     default: 
      textView.setText("Result doesn't match the codes available..."); 
     break; 

     } 

    } 
    catch (JSONException e) { 
    e.printStackTrace(); 
    } 
    catch (ClientProtocolException e) { 
    e.printStackTrace(); 
    } 
    catch (IOException e) { 
    e.printStackTrace(); 
    } 

} 

private StringBuilder inputStreamToString(InputStream is) { 
     String rLine = ""; 
     StringBuilder answer = new StringBuilder(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 

     try { 
     while ((rLine = rd.readLine()) != null) { 
      answer.append(rLine); 
      } 
     } 

     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return answer; 
     } 

응용 프로그램은 오류없이 작동하고 있지만, switch 문에서 어떤 결과를 표시하지 않습니다. 나는 문제가 어디 있는지 모른다. 여기 누군가는 똑같은 문제를 겪었을 수도 있습니다. 그러니 제발 도와주세요.

감사합니다.

+0

scanResult가 선언되지 않았습니다. 'ret_id'에 저장소 ID 값을 가지고 있기 때문에 어떻게 보이길 원하십니까 – MDMalik

+0

scanResult가 선언되었습니다. 게시물을 편집하겠습니다. – Saraz

+0

이제 123456처럼 scanresult에 저장된 값은 무엇입니까? – MDMalik

답변

1

JSON 구조가 유효하지 않습니다.

는 [],
{"menu": { 
    "id": "file", 
    "value": "File", 
    "popup": { 
    "menuitem": [ 
     {"value": "New", "onclick": "CreateNewDoc()"}, 
     {"value": "Open", "onclick": "OpenDoc()"}, 
     {"value": "Close", "onclick": "CloseDoc()"} 
    ] 
    } 
}}

JSON이 {}를 기반으로이 구조에 보라, :,,

난 당신이 jArray = object.getJSONArray("barCodes");을 확인하고 로그를 인쇄하려고 주장한다. jArray.toString();

어레이 내부에 무엇이 있는지 정확히 알 수 있습니다.

관련 문제