【问题标题】:Wrong JSON from PHP来自 PHP 的错误 JSON
【发布时间】:2015-10-28 19:52:46
【问题描述】:

我正在创建一个类,其唯一目的是从 MySQL DB 在线获取一些数据。

我已经成功创建了 PHP 文件,它的效果非常好,并返回了我需要的所有四个字段 (http://marcodr.byethost7.com/androidpit.php)

代码本身相当简单:

<?php
$con=mysql_connect("xxxxxx","xxx","xxxx");

if(!$con)
die('could not connect: ' .mysql_error());

mysql_select_db("xxxx",$con);

$result = mysql_query("SELECT * FROM `xxx` where xxxx = 'xxxxx'");

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

print(json_encode($output));

mysql_close($con);

?>

下一步是让 java 类解析 JSON 数据

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends Activity {
/** Called when the activity is first created. */

String line=null;
String [] stream_name;
HttpURLConnection urlConnection = null;
String value;
ProgressDialog progressDialog;
private static final String TAG_ID = "ID";
private static final String TAG_Price = "Price";
private static final String TAG_Weight = "Weight";
private static final String TAG_PW = "PW";

TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.enableDefaults(); //STRICT MODE ENABLED
    resultView = (TextView) findViewById(R.id.result);
}

public void getData(){
    String result = "";
    InputStream isr = null;
    try{
        URL url = new URL ("http://marcodr.byethost7.com/fahmi.php");
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();
        isr = urlConnection.getInputStream();

    }
    catch(Exception e){
        Log.e("log_tag", "Error in http connection " + e.toString());
        resultView.setText("Couldn't connect to database");
    }
    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        isr.close();

        result=sb.toString();
    }
    catch(Exception e){
        Log.e("log_tag", "Error  converting result " + e.toString());
    }

    //parse json data
    try {
        String s = "";
        JSONArray jArray = new JSONArray(result);

        for(int i=0; i<jArray.length();i++){
            JSONObject json = jArray.getJSONObject(i);
            s = s +
                    "Price : "+json.getString("Price")+"\n"+
                    "Weight : "+json.getString("Weight")+"\n"+
                    "Price/Weight : "+json.getString("PW")+"\n\n";

        }

        resultView.setText(s);

    } catch (Exception e) {
        // TODO: handle exception
        Log.e("log_tag", "Error Parsing Data "+e.toString());
    }
}

}

Manifest 文件已授权,布局中唯一的 TextView 已设置,但它仍然无法获取所选信息,我为这个麻烦花费了数天时间。

【问题讨论】:

  • 请发布您的 json 以及您的 logcat
  • 到底是什么问题?这是在哪里失败以及产生了哪些错误?
  • 这些 MySQL 函数已经过时了。您应该考虑使用 PDO 类。
  • @Aakash 这是我的 logcat pastebin.com/75vipeTK 我猜 JSON 在课堂活动中?
  • @drschultz 我希望用解析的数据填充 Textview。如果我从浏览器打开 php,它可以完美运行,但问题是我无法将此文本放入 Android 应用程序

标签: php android mysql json


【解决方案1】:

根据您的 Android 代码,这是因为您无法在主线程上运行 http 连接。

查看this 示例以进一步了解它

【讨论】:

  • 嗨@Ralph,这只会让事情变得更糟:(pastebin.com/GSdS8ZRb看看它是如何得到的marcodr.byethost7.com/androidpitPDO.php
  • 算了。我在 WHERE 条件下管理 PDO。然而,如果我在应用程序中链接这个新的 php,它仍然不会加载 Empty LogCat: Not-enabled CheckJNI (already on) D/: HostConnection::get() New Host Connection created 0xb9075a80, tid 839 W/EGL_emulation: eglSurfaceAttrib 未实现 D/OpenGLRenderer:启用调试模式 0
猜你喜欢
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 2020-11-26
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多