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 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥15 MCNP里如何定义多个源?
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services