Ich habe eine Methode, aus dem ich die Daten aus webservice bin holen.JSONException: mit http get in Android-Wert <xml vom Typ java.lang.String kann nicht in JSONObject umgewandelt wird
Hier ist mein Code:
protected Void doInBackground(Void... arg0){
HttpHandler sh = new HttpHandler();
// making request to url and getting respose
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " +jsonStr);
if (jsonStr != null){
try {
JSONObject jsonObject = new JSONObject(jsonStr);
// getting json array node
JSONArray shipments = jsonObject.getJSONArray("string");
// looping through all shipments
for (int i = 0; i < shipments.length(); i++){
JSONObject c = shipments.getJSONObject(i);
String id = c.getString("ID");
String controlnumber = c.getString("ControlNumber");
String clientcn = c.getString("clientcn");
String chargeableweight = c.getString("ChargeableWeight");
// tmp hashmap for single shipmentdetail
HashMap<String, String> shipment = new HashMap<>();
// adding each child nodeto hashmap
shipment.put("id", id);
shipment.put("controlnumber", controlnumber);
shipment.put("clientcn", clientcn);
shipment.put("chargeableweight", chargeableweight);
// adding shipment to shipment list
shipmentList.add(shipment);
}
}catch (final JSONException e){
Log.e(TAG, "Json parsing error: " +e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " +e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
}else {
Log.e(TAG, "Couldn't get Json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
public String makeServiceCall(String reqUrl){
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
//read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
}catch (MalformedURLException e){
Log.e(TAG, "MalformedException: " +e.getMessage());
}catch (ProtocolException e){
Log.e(TAG, "Protocal Exception: " +e.getMessage());
}catch (IOException e){
Log.e(TAG, "IOException: " +e.getMessage());
}catch (Exception e){
Log.e(TAG, "Exception: " +e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is){
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null){
sb.append(line).append('\n');
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
is.close();
}catch (IOException e){
e.printStackTrace();
}
}
return sb.toString();
}
Meine Web-Service, um die Daten in diesem Format zurückgibt:
Response from url: <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">[{"ID":144412,"ControlNumber":186620,"clientcn":160054,"ChargeableWeight":1.00,"TotalPieces":1,"SpecialPickup":false,"ReadyDate":null,"CompanyName":"233/Evergreen","CompanyAddress":"582 Tuna Street","CompanyAddress1":"45288","City":"Terminal Island","State":"CA","ZipCode":"90731","ContactPhone":"","ContactName":"","C_CompanyName":"Mitoy Logistics","C_CompanyAddress":"1140 Alondra blvd","C_CompanyAddress1":"","C_City":"Compton","C_State":"CA","C_ZipCode":"90220","C_ContactPhone":"","C_ContactName":"John ","priority":5,"FreightShipment":false,"FreightDetails":"20 STD CNTR# SCLU7888484"}]</string>
Wie die Antwort auf JSON-Objekt in Android konvertieren? Das kostet mir wertvolle Zeit und nicht weiter. Ich stecke hier fest.
Jede Idee oder Anregungen wenden Sie sich bitte!
Vielen Dank im Voraus ..
Sie müssen nur die XML-Tags von den Enden Ihrer Antwort zu bekommen. Dann entschlüsseln Sie das Innere. –
Wie kann ich das tun, nachdem Antwort erhalten habe, kann ich trim String-Methode dafür verwenden? –
Wer jemals diese "brillante" Idee hatte, XML mit JSON zu mischen, sollte Ihnen gesagt haben, dass Sie zuerst das XML-Dokument analysieren müssen (mit XPATH o.ä.), bevor Sie die Nutzdaten an einen JSON-Parser übergeben. Grüße an diesen "Helden des Internetzeitalters". –