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

我已经成功创建了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());
    }
}

}


清单文件已被授权,仅在布局中设置了TextView,但仍然无法获取所选信息,因此我为此花费了很多天。

最佳答案

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

查看this示例以了解更多信息

07-28 02:18
查看更多