[分享]android使用php连接到mysql_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2146 | 回复: 0   主题: [分享]android使用php连接到mysql        下一篇 
wlcbhrss
注册用户
等级:新兵
经验:68
发帖:65
精华:0
注册:2011-8-21
状态:离线
发送短消息息给wlcbhrss 加好友    发送短消息息给wlcbhrss 发消息
发表于: IP:您无权察看 2014-12-19 14:59:12 | [全部帖] [楼主帖] 楼主

我试图得到一个变量数据库和我得到力量:

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>


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

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论