dselp3944 2015-10-26 20:11
浏览 46

如何通过PHP将我的Android应用程序连接到MySQL

I would like to connect my android app to a MySQL table I have on Hostinger. The layout is really simple: a simple TextView whose text has to be filled through a string with the setText.

I made sure I used the most up to date library (HttpURLConnection), that the PHP file on the server is well done, that I properly parse and that the app can connect to the internet through specification in the Manifest file.

Yet, when I open the app, I just see a black page and there is no error on the logcat. Any idea of what could it be?

This is my Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

while this is my MainActivity

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

String line=null;
String [] stream_name;
HttpURLConnection urlConnection = null;
String value;

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://31.170.165.234/public_html/AH_Bread2.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 + "
");
        }
        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")+"
"+
                    "Weight : "+json.getInt("Weight")+"
"+
                    "Price/Weight : "+json.getString("P/W")+"

";

        }

        resultView.setText(s);

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

}

And this is my PHP file on the server

<?php

$mysqli = NEW MySQL('marcodr.byethost7.com','xxxxxx','xxxxxx','xxxxxx');

$resultSet = $mysqli->query("SELECT * FROM TABLE 1 WHERE ID = 'xxxx'");

if($resultSet->num_rows != 0){

while($rows = $resultSet->fetch_assoc())
{
$ID = $rows['ID'];
$Price = $rows['Price'];
$Weight = $rows['Weight'];
$PW = $rows['PW'];


print(json_encode($rows);
}
}else{
echo "No results.";
}
?>

As you can see, my database is really simple: only four columns, properly named "ID", "Price", "Weight" and "P/W".

I would simply want to set the text of the TextView (identified through the FindViewById) to the string "s" defined by the end of the Java class, but it's not working despite all the checks! Any idea?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大