2014-06-27 29 views
-1

每次我得到这个错误:E/log_tag(1601):错误的数据进行解析org.json.JSONException:值<java.lang.String类型的DOCTYPE不能当我开始我的程序转换成JSONArray

E/log_tag(1601):解析数据时出错org.json.JSONException:值!无法将类型java.lang.String的DOCTYPE转换为JSONArray

我读了很多关于它的事情,但并不知道如何我可以解决问题.. 请hep us !!!谢谢!

java代码:

package com.example.vertretungsplannonnenwerth; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONObject; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v4.app.Fragment; 
import android.annotation.SuppressLint; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 


@SuppressLint("NewApi") 
public class MainActivity extends ActionBarActivity { 

TextView resultView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    StrictMode.enableDefaults(); 
    resultView = (TextView) findViewById(R.id.result); 


getData(); 


} 






public void getData(){ 
    String result = ""; 
    InputStream is = null; 
    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("link"); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    } 

catch(Exception e){ 
    Log.e("log_tag", "Keine Verbindung zur Internetseite"+e.toString()); 
    resultView.setText("Keine Verbindung zur Datenbank"); 
    } 

try{ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"),8); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while ((line = reader.readLine()) != null){ 
     sb.append(line + "\n"); 
    } 
    is.close(); 

    result=sb.toString(); 
} 
catch (Exception e){ 
    Log.e("log_tag", "Fehler beim Konvertieren" +e.toString()); 
} 
try{ 
    String s =result; 
    JSONArray jArray = new JSONArray(result); 


    for(int i=0; i<jArray.length(); i++){ 
     JSONObject json = jArray.getJSONObject(i); 
     s = s + 
       "Datum : "+json.getInt("Datum") + "\n" + 
       "Stunde : "+json.getInt("Std")+ "\n" + 
       "Klasse : "+json.getInt("Klasse")+ "\n" + 
       "Fach : "+json.getString("Fach")+ "\n" + 
       "Lehrer : "+json.getString("Lehrer")+ "\n" + 
       "Vertretung : "+json.getString("Vertretung")+ "\n" + 
       "Raum : "+json.getInt("Raum")+ "\n" + 
       "Bemerkung : "+json.getString("Bemerkung")+ "\n\n"; 

    } 
    resultView.setText(s); 

} 
catch (Exception e) { 
    Log.e("log_tag", "Fehler beim Datenabruf"+e.toString()); 
} 




} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, 
       false); 
     return rootView; 
    } 
} 

}

PHP代码:

$con = mysql_connect("server", "db name", "password"); 

if (!$con) 
    { 
    die ('Keine Verbindung zur Datenbank.': mysql_error()); 
    } 
mysql_select_db("db", $con); 

$result=mysql_query("SELECT*FROM tbVertretungsplan"); 

while ($row = mysql_fetch_assoc($result)) 
    { 
    $output[]=$row; 
    } 

print (json.encode($output)); 

mysql_close($con); 


?> 

回答

0

您要开始转换字!DOCTYPE JSON字符串。 由于字符串!DOCTYPE未被格式化为JSON字符串,因此会出现JSONException。

您正在从数据中读取数据的链接从HTML标记<!DOCTYPE html>example)开始。清理您的数据输入字符串并删除任何HTML标记和非JSON字符串以使您的代码正常工作。您可以在您的php代码中进行清理(不知道如何使用您提供的代码执行此操作),也可以清除应用中的所有标记。

要删除HTML标记(和所有其他标签),您可以使用:

String noHTML = html.replaceAll("\\<.*?>",""); 
+0

我tryed它,但它不工作,要么... – user3781172

+0

你能分析它,并在问题粘贴之前,打印您的JSON ? – Pphoenix

相关问题