2016-07-15 37 views
0

我的代码被成功地检索JSON数据,但在应用中由于错误不显示org.json.jsonarray不能转换到的JSONObject。 我尝试过使用JSON数组,但那是触发其他错误的链。 请帮助:org.json.jsonarray不能转换到的JSONObject误差获得用于JSON数据GET操作

这里是我的代码:

public class Main7Activity extends AppCompatActivity { 
    public String username; 
    public String result; 
    EditText contents; 
    //public String result; 
    public EditText serial1; 
    public String serial; 
    StringBuilder content = new StringBuilder(); 

    TextView myResultTxt; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main7); 
     Intent intentExtras = getIntent(); 
     Bundle extraBundle = intentExtras.getExtras(); 
     username = extraBundle.getString("username"); 
     serial1 = (EditText) findViewById(R.id.serial); 
     serial = serial1.getText().toString(); 
     contents = (EditText) findViewById(R.id.contents); 
     Button ret = (Button) findViewById(R.id.ret); 

     myResultTxt = (TextView) findViewById(R.id.my_result); 

    } 

    public void ret(View v) { 

     serial = serial1.getText().toString(); 

     if (TextUtils.isEmpty(serial)) { 
      serial1.setError("Please Enter the Serial Number"); 
     } else { 
      try { 
       Log.w("var23", "api hit in start"); 
       GetClass apiObj = new GetClass(this); 
       apiObj.url = "http://10.43.106.94:8080/SRNSmartLab/rest/service/getNEdata" + serial; 
//    apiObj.displayParm = "origin"; 
       apiObj.execute(); 
       Log.w("var23", "api hit in executed"); 

      } catch (Exception e) { 
       result = e.getMessage(); 
       Toast.makeText(Main7Activity.this, "" + result, Toast.LENGTH_LONG).show(); 

      } 

     } 
    } 


    /* APi caller */ 
    private class GetClass extends AsyncTask<String, Void, Void> { 

     private final Context context; 

     public String url; 
     public String displayParm; 

     public GetClass(Context c) { 
      this.context = c; 
     } 

     protected void onPreExecute() { 
//  progress= new ProgressDialog(this.context); 
//  progress.setMessage("Loading"); 
//  progress.show(); 
      Log.w("var23", "loading"); 
     } 

     @Override 
     protected Void doInBackground(String... params) { 
      try { 
       Log.w("var23", "doInBackground url : " + this.url); 
//   final TextView outputView = (TextView) findViewById(R.id.showOutput); 
       URL url = new URL(this.url); 
       HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
//    String urlParameters = "fizz=buzz"; 
       connection.setRequestMethod("GET"); 
       connection.setRequestProperty("USER-AGENT", "Mozilla/5.0"); 
       connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5"); 

       int responseCode = connection.getResponseCode(); 
       Log.w("var23", "api hit in respomse"+String.valueOf(responseCode)); 
       final StringBuilder output = new StringBuilder("Request URL " + url); 
       output.append(System.getProperty("line.separator") + "Response Code " + responseCode); 
       output.append(System.getProperty("line.separator") + "Type " + "GET"); 
       BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
       String line = ""; 
       StringBuilder responseOutput = new StringBuilder(); 
       System.out.println("output===============" + br); 
       while ((line = br.readLine()) != null) { 
        responseOutput.append(line); 
       } 
       Log.w("var23", "Result from API: " + responseOutput.toString()); 
       br.close(); 

       final JSONObject jObject = new JSONObject(responseOutput.toString()); 
//    final String myUrl = jObject.getString(this.displayParm); 

       String theData = System.getProperty("line.separator") + "Response " + System.getProperty("line.separator") + System.getProperty("line.separator") + responseOutput.toString(); 
       Log.w("var24", "data: " + theData); 
       output.append(theData); 

       Main7Activity.this.runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         // To display any content to ui 
         try { 
          myResultTxt.setText(jObject.getString("url")); 
          contents.setText(jObject.toString()); 
          Log.w("var25", jObject.toString()); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
//     outputView.setText(output); 
//     progress.dismiss(); 

        } 
       }); 

      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       Log.w("var26", "error MalformedURLException"); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       Log.w("var27", "error IOException"); 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       Log.w("var28", "error JSON exception"); 
       e.printStackTrace(); 
      } 
      Log.w("var23", "end of the line"); 
      return null; 
     } 
    } 

JSON输出:从AndroidStudio 输出:从API结果:[{ “_id”:{ “$ OID”: “5784818bcb30b4918964b50f”},“ LabLocation“:”U02“,”RackLocation“:”EOI-Radio-B01“,”ShelfLocation“:”SH-01“,”VER“:”908762“,”Cluster“:”Radio“,”Name“ BTS01-Coral-yun-SEW“,”SoftwareVersion“:”LN6.0“,”HardwareType“:”FRGP“,”AssetNo“:”108243“,”SerialNO“:”RY114208612“,”Location“:”zzccg“ “UNAME”: “addtvI”}]

堆栈跟踪:

07-15 10:07:28.750 6240-6240/com.example.manv.nokialabs W/var23: api hit in start 
07-15 10:07:28.755 6240-6240/com.example.manv.nokialabs W/var23: loading 
07-15 10:07:28.756 6240-6240/com.example.manv.nokialabs W/var23: api hit in executed 
07-15 10:07:28.756 6240-6290/com.example.manv.nokialabs W/var23: doInBackground url : http://10.43.106.94:8080/SRNSmartLab/rest/service/getNEdataRY114208612 
07-15 10:07:28.758 6240-6290/com.example.manv.nokialabs W/System: ClassLoader referenced unknown path: /system/framework/tcmclient.jar 
07-15 10:07:28.802 6240-6290/com.example.manv.nokialabs W/var23: api hit in respomse200 
07-15 10:07:28.804 6240-6290/com.example.manv.nokialabs I/System.out: [email protected] 
07-15 10:07:28.805 6240-6290/com.example.manv.nokialabs W/var23: Result from API: [{ "_id" : { "$oid" : "5784818bcb30b4918964b50f"} , "LabLocation" : "U02" , "RackLocation" : "EOI-Radio-B01" , "ShelfLocation" : "SH-01" , "VER" : "908762" , "Cluster" : "Radio" , "Name" : "BTS01-Coral-yun-SEW" , "SoftwareVersion" : "LN6.0" , "HardwareType" : "FRGP" , "AssetNo" : "108243" , "SerialNO" : "RY114208612" , "Location" : "zzccg" , "Uname" : "addtvI"}] 
07-15 10:07:28.807 6240-6290/com.example.manv.nokialabs W/var28: error JSON exception 
07-15 10:07:28.808 6240-6290/com.example.manv.nokialabs W/System.err: org.json.JSONException: Value [{"_id":{"$oid":"5784818bcb30b4918964b50f"},"LabLocation":"U02","RackLocation":"EOI-Radio-B01","ShelfLocation":"SH-01","VER":"908762","Cluster":"Radio","Name":"BTS01-Coral-yun-SEW","SoftwareVersion":"LN6.0","HardwareType":"FRGP","AssetNo":"108243","SerialNO":"RY114208612","Location":"zzccg","Uname":"addtvI"}] of type org.json.JSONArray cannot be converted to JSONObject 
07-15 10:07:28.808 6240-6290/com.example.manv.nokialabs W/System.err:  at org.json.JSON.typeMismatch(JSON.java:111) 
07-15 10:07:28.808 6240-6290/com.example.manv.nokialabs W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:160) 
07-15 10:07:28.808 6240-6290/com.example.manv.nokialabs W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:173) 
07-15 10:07:28.808 6240-6290/com.example.manv.nokialabs W/System.err:  at com.example.albbaby.nokialabs.Main7Activity$GetClass.doInBackground(Main7Activity.java:172) 
07-15 10:07:28.808 6240-6290/com.example.manv.nokialabs W/System.err:  at com.example.albbaby.nokialabs.Main7Activity$GetClass.doInBackground(Main7Activity.java:127) 
07-15 10:07:28.808 6240-6290/com.example.manv.nokialabs W/System.err:  at android.os.AsyncTask$2.call(AsyncTask.java:295) 
07-15 10:07:28.809 6240-6290/com.example.manv.nokialabs W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
07-15 10:07:28.809 6240-6290/com.example.manv.nokialabs W/System.err:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
07-15 10:07:28.809 6240-6290/com.example.manv.nokialabs W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
07-15 10:07:28.809 6240-6290/com.example.manv.nokialabs W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
07-15 10:07:28.809 6240-6290/com.example.manv.nokialabs W/System.err:  at java.lang.Thread.run(Thread.java:818) 
07-15 10:07:28.809 6240-6290/com.example.manv.nokialabs W/var23: end of the line 
+0

显示自AndroidStudio你得到JSON和完整的堆栈跟踪 – Jens

+0

输出:从API 结果:[{ “_id”:{ “$ OID”: “5784818bcb30b4918964b50f”}, “LabLocation”: “U02”, “RackLocation”:“EOI-Radio-B01”,“ShelfLocation”:“SH-01”,“VER”:“908762”,“Cluster”:“Radio”,“Name”:“BTS01-Coral-yun- “SoftwareVersion”:“LN6.0”,“HardwareType”:“FRGP”,“AssetNo”:“108243”,“SerialNO”:“RY114208612”,“Location”:“zzccg”,“Uname”:“addtvI “}] – Alby

回答

0

效应初探包含JsonArray但你想转换成JSONObject的那方式,示值误差(org.json.jsonarray不能被转换为JSONObject的。)

以前的代码:

final JSONObject jObject = new JSONObject(responseOutput.toString()); 

更改它,如下行:

final JSONArray jArrayObj= new JSONArray (responseOutput.toString()); 
+0

更改后代码正常工作 – Alby

0

您试图将JSONArray转换为JSONObject。

使用此

final JSONArray jArray = new JSONArray(responseOutput.toString()); 
相关问题