Mein Code ruft die json-Daten erfolgreich ab, wird aber aufgrund des Fehlers nicht in der App angezeigt. Org.json.jsonarray kann nicht in jsonobject konvertiert werden. Ich habe versucht, JSONarray zu verwenden, aber das löst eine Kette von anderen Fehlern aus. Bitte helfen:org.json.jsonarray kann nicht in jsonobject konvertiert werden. Fehler wird für json-Daten erhalten. GET-Operation
hier ist mein Code:
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 Ausgabe: Ausgabe von Android Studio: Ergebnis 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 "," Hardwaretyp ":" FRGP "," AssetNo ":" 108243 "," SerialNO ":" RY114208612 "," Standort ":" zzccg " , "Uname": "addtvI"}]
Stapel-Trace:
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
Zeigen Sie die json Sie erhalten und die komplette stacktrace – Jens
Ausgabe von Android Studio: Ergebnis aus 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 "," Hardwaretyp ":" FRGP "," AssetNo ":" 108243 "," SerialNO ":" RY114208612 "," Standort ":" zzccg "," Uname ":" addtvI "}] – Alby