2016-05-24 9 views
1

Ich trat von Abrufen von Daten aus einem Hosting umgewandelt werden, aber wenn ich die App in meinem Handy ausführen, bekomme ich einen Fehler,Fehler JSONException: Der Wert des Typs java.lang.String kann nicht auf JSONObject

Fehler org .json.JSONException: Wert < html> < body> < Skript vom Typ java.lang.String kann nicht in JSONObject umgewandelt werden Hier sind meine Variablen und der Code, der die JSON-Datei analysiert:

$host=""; 
$username=""; 
$password=""; 
$db_name=""; 

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 
$sql = "select * from pedidos"; 
$result = mysql_query($sql); 
$json = array(); 

if(mysql_num_rows($result)){ 
while($row=mysql_fetch_assoc($result)){ 
$json['users1'][]=$row; 
} 
} 
mysql_close($con); 
echo json_encode($json); 
?> 

und Diese JSON-Klasse

private String jsonResult; 
private String url = "http://proyectocul.mipropia.com/jsondiego/pedidos.php"; 
private ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_verpedido); 
    listView = (ListView) findViewById(R.id.listviewpedido); 
    accessWebService(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

// Async Task to access the web 
private class JsonReadTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... params) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(params[0]); 
     try { 
      HttpResponse response = httpclient.execute(httppost); 
      jsonResult = inputStreamToString(
        response.getEntity().getContent()).toString(); 
     } 

     catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    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(); 
      Toast.makeText(getApplicationContext(), 
        "Error..." + e.toString(), Toast.LENGTH_LONG).show(); 
     } 
     return answer; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     ListDrwaer(); 
    } 
}// end async task 

public void accessWebService() { 
    JsonReadTask task = new JsonReadTask(); 
    // passes values for the urls string array 
    task.execute(new String[]{url}); 
} 

// build hash set for list view 
public void ListDrwaer() { 
    List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>(); 

    try { 
     JSONObject jsonResponse = new JSONObject(jsonResult); 
     JSONArray jsonMainNode = jsonResponse.optJSONArray("users1"); 


     for (int i = 0; i < jsonMainNode.length(); i++) { 
      JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
      String name = jsonChildNode.optString("id"); 
      String number = jsonChildNode.optString("producto"); 
      String user = jsonChildNode.optString("username"); 
      String cantidad = jsonChildNode.optString("cantidad"); 
      String hora = jsonChildNode.optString("created_at"); 
      String outPut = "N°" + name + " - " + "Usuario: " + user + " - " + "Producto: " + number + " - " + "Cantidad: " + cantidad + " - " + "Fecha: " + hora; 
      employeeList.add(createEmployee("employees", outPut)); 
     } 
    } catch (JSONException e) { 
     Toast.makeText(getApplicationContext(), "Error" + e.toString(), 
       Toast.LENGTH_SHORT).show(); 
    } 

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList, 
      android.R.layout.simple_list_item_1, 
      new String[] { "employees" }, new int[] { android.R.id.text1 }); 
    listView.setAdapter(simpleAdapter); 
} 

private HashMap<String, String> createEmployee(String name, String number) { 
    HashMap<String, String> employeeNameNo = new HashMap<String, String>(); 
    employeeNameNo.put(name, number); 
    return employeeNameNo; 
} 

}

wo ist mein Fehler ??

+0

Zeichenfolge name = jsonChildNode.optString ("id", null); –

+1

hinzufügen das ?? @MilosLulic –

Antwort

0

Es scheint, dass Ihr jsonString nicht das ist, was Sie erwarten. Sie könnten es ausdrucken oder in debug stoppen, um es zu untersuchen. Es ist möglich, dass Sie eine Fehlermeldung vom Webserver erhalten, z. B. 404 nicht gefunden in einer HTML-Seite, oder Sie haben einen Fehler auf Ihrer Serverseite, oder Ihr Server unterstützt GET und nicht POST. Schwer zu sagen.

Verwandte Themen