【发布时间】: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 应用程序