android使用php连接到mysql

时间:2013-11-16 12:31:03

标签: php android mysql

我正试图从我的数据库中获取一个变量,并且我正在关闭力量:

    11-16 16:33:41.757: E/AndroidRuntime(26305): FATAL EXCEPTION: main
11-16 16:33:41.757: E/AndroidRuntime(26305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databaseexample/com.example.databaseexample.MainActivity}: java.lang.NullPointerException
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.os.Looper.loop(Looper.java:130)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.main(ActivityThread.java:3687)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at java.lang.reflect.Method.invoke(Method.java:507)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at dalvik.system.NativeStart.main(Native Method)
11-16 16:33:41.757: E/AndroidRuntime(26305): Caused by: java.lang.NullPointerException
11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONTokener.nextValue(JSONTokener.java:90)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONArray.<init>(JSONArray.java:87)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONArray.<init>(JSONArray.java:103)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at com.example.databaseexample.MainActivity.onCreate(MainActivity.java:100)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-16 16:33:41.757: E/AndroidRuntime(26305):    ... 11 more

我的机器人部分:

public class MainActivity extends ListActivity {

    private List nameValuePairs;
    private JSONArray jArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String result = null;

         InputStream is = null;

         StringBuilder sb=null;

         String result1=null;

         //http post

         try{

         HttpClient httpclient = new DefaultHttpClient();

         HttpPost httppost = new HttpPost("http://lab.sif.mruni.eu/~sanevys/parduotuve/puslapiai/android.php");

         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         HttpResponse response = httpclient.execute(httppost);

         HttpEntity entity = response.getEntity();

         is = entity.getContent();

         }catch(Exception e){

         Log.e("log_tag", "Error in http connection"+e.toString());

         }

         //convert response to string

         try{

         BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);

         sb = new StringBuilder();

         sb.append(reader.readLine() + "\n");

         String line="";

         while ((line = reader.readLine()) != null) {

         sb.append(line + "\n");

         }

         is.close();

         result1=sb.toString();

         }catch(Exception e){

         Log.e("log_tag", "Error converting result "+e.toString());

         }

         //paring data

         int fd_id;

         String fd_name;

         try{

         jArray = new JSONArray(result);

         JSONObject json_data=null;

         for(int i=0;i<jArray.length();i++){

         json_data = jArray.getJSONObject(i);

         fd_id=json_data.getInt("FOOD_ID");

         fd_name=json_data.getString("FOOD_NAME");

         }

         }catch(JSONException e){

         Toast.makeText(getBaseContext(), "No Food Found", Toast.LENGTH_LONG).show();

         }catch (ParseException e){

         e.printStackTrace();

         }

         }

}

我的php部分:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_lab = "localhost";
$database_lab = "database";
$username_lab = "username";
$password_lab = "password";
$lab = mysql_pconnect($hostname_lab, $username_lab, $password_lab) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db("database");
  $sql=mysql_query("select * from FOOD where FOOD_NAME like 'A%'");
  while($row=mysql_fetch_assoc($sql)) $output[]=$row;
  print(json_encode($output));
  mysql_close();
?>

我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
<ListView         
    android:id="@android:id/list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>
</RelativeLayout>

任何帮助/建议将不胜感激:)

1 个答案:

答案 0 :(得分:1)

由于您要从ListActivity ListView id扩展您的活动,因此 android:id="@android:id/list"

ListView

检查您是否在xml文件中提供了正确的ID

或者,如果您尚未向xml文件添加ListView,请添加id以及上述ListActivityListView的所有子类都应该有{{1}}。