Java 分析数据时出错,JSON

Java 分析数据时出错,JSON,java,android,database,json,Java,Android,Database,Json,我正在尝试执行一个小的android应用程序,从托管在web服务上的数据库中获取数据,其中包含以下大量代码,但出现了一些错误 MainActivity.java public class MainActivity extends Activity { TextView Name; TextView ID; TextView Email; private static String url = "http://HostName/GetData.php"; private static fin

我正在尝试执行一个小的android应用程序,从托管在web服务上的数据库中获取数据,其中包含以下大量代码,但出现了一些错误

MainActivity.java

  public class MainActivity extends Activity {

TextView Name;
TextView ID;
TextView Email;
private static String url = "http://HostName/GetData.php";
private static final String TAG_Name = "Name";
private static final String TAG_ID = "Id";
private static final String TAG_Email = "Email";

JSONArray user = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new JSONParse().execute();

}

@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;
}
private class JSONParse extends AsyncTask<String, String, JSONObject>
{
    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        Name = (TextView) findViewById(R.id.textView4);
        ID = (TextView) findViewById(R.id.textView5);
        Email = (TextView) findViewById(R.id.textView6);

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
               // Getting JSON Array
               user = json.getJSONArray(TAG_Name);
               JSONObject c = user.getJSONObject(0);

               // Storing  JSON item in a Variable
               String Name1 = c.getString(TAG_Name);
               String ID1 = c.getString(TAG_ID);
               String Email1 = c.getString(TAG_Email);

               //Set JSON Data in TextView
               Name.setText(Name1);
               ID.setText(ID1);
               Email.setText(Email1);

       } catch (JSONException e) {
           e.printStackTrace();
       }

    }

}
 }
}

我有以下日志:

PHP文件:

 public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    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();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
}
<?php
header('Content-type=application/json; charset=utf-8');
$con = mysql_connect("***","***","***");

if (!$con)
    {
    die('Could not Connect:' . mysql_error());
    }

mysql_select_db("database_name",$con);

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

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

print(json_encode($output));

mysql_close($con);

?>

PHP的回应是:


[{“姓名”:“Izzo32”,“Id”:“5554”,“电子邮件”:”xxx@hotmail.com“}]

如果要解析JSON,那么应该提供JSON,而不是html。显然,您收到了一个“”标记


如果我是你,我会修复我的Web服务并使用它将json转换为java对象

看起来您正试图将非JSON格式的字符串转换为JSON对象,而这正是导致错误的原因。您应该打印出html响应以查看整个内容,并将其发布在此处。

再看看这个问题,我很确定我的专业知识没有什么用处。很抱歉。

但是您试图转换为json对象的字符串是什么。可能是在html括号内,我不明白/将您试图转换为json数组或json对象的字符串打印到日志cat。从那里你可以看到它是否有html括号,如果它或者它的格式不正确。我已经这样做了,你可以在问题中找到LogCat,我猜它有一个html括号,但我不知道如何处理这种情况!也许可以尝试删除php顶部的header()声明。我认为在任何脚本中,我都不会将json返回给我使用的客户端。我唯一一次这样做是在我使用php返回图像或文件时。谢谢,我会这样做:)您的Web服务响应是什么?你能把它粘贴到这里吗?问题中已经发布了PHP响应,我已经解决了一个类似的问题,谢谢:)但是错误代码说它得到了一个标签。。。你解决了吗?