0

I have an Android app that uses bar code scanner library. All is working good, until I have created an Activity that is called after the scanning is done.

That Activity uses this JSON array:

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

echo json_encode($barCodes); ?>

and the code to parse this JSONArray is:

   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;
       }

app is working without errors, but it is not displaying any result from the switch statement. I do not know where is the problem. Someone here may have had the same problem, so please help me.

Thanks.

7
  • You scanResult is not declared. and how do you want it to look because you have store ID value in ret_id Commented Jun 22, 2013 at 17:01
  • scanResult is declared, let me edit the post. Commented Jun 22, 2013 at 17:06
  • now what is the value stored in the scanresult like could it be 123456? Commented Jun 22, 2013 at 17:09
  • I just need the id and format pair to be printed. Commented Jun 22, 2013 at 17:10
  • yes the scanner returns the value scanned.. like those 3 in switch statement Commented Jun 22, 2013 at 17:11

1 Answer 1

1

What I feel is your JSON structure is not valid.

Have a look on this structure

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

JSON is based on {}, [], :, ,

I insist you check the jArray = object.getJSONArray("barCodes"); and try to print the log. like jArray.toString();

You will know exactly what the array is having inside.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.