有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 E/log_标签(1601):解析数据组织时出错。json。JSONException:值<!java类型的DOCTYPE。无法将lang.String转换为JSONArray

每次启动程序时都会出现以下错误:

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

我读了很多关于它的书,但不知道如何解决这个问题。。 请帮助我!!!谢谢

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 安卓.support.v7.app.ActionBarActivity;
import 安卓.support.v4.app.Fragment;
import 安卓.annotation.SuppressLint;
import 安卓.os.Bundle;
import 安卓.os.StrictMode;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.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);


?>

共 (1) 个答案

  1. # 1 楼答案

    您试图转换的JSON字符串以单词!DOCTYPE开头。 由于字符串!DOCTYPE未格式化为JSON字符串,因此会得到一个JSONException

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

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

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