0

我是安卓新手。我想使用 PHP 连接到 MySQL 服务器。

我有一个交易数据库。在这个数据库中,我创建了一个表category。该表由两列组成。首先是category_id,它具有 auto_increment 和 primary_key 并且具有数据类型 INT。第二列是category,其数据类型为 VARCHAR(30)。

我的 PHP 脚本

<?php
$con = mysql_connect("127.0.0.1","root","");
if (!$con) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("deal",$con);
$sql=mysql_query("SELECT *  FROM category ORDER BY `category`.`category` ASC");
while($row=mysql_fetch_assoc($sql))
  $output[]=$row;
  print(json_encode($output));
mysql_close($con);
?>

我的安卓代码:

package com.example.city;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String result = null;
InputStream is = null;
StringBuilder sb = null;
ArrayList nameValuePairs = new ArrayList();  
List r = new ArrayList();
try{
      //http post
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://127.0.0.1/city.php");
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);
      HttpEntity entity = response.getEntity();
      is = entity.getContent();
    }
    catch(Exception e){
      Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
}
//Convert response to string
try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
      sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
      is.close();
      result = sb.toString();
}
catch(Exception e) {
      Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
    }
//END Convert response to string
try{
      JSONArray jArray = new JSONArray(result);
      JSONObject json_data=null;
      for(int i=0;i<jArray.length();i++ ){
        json_data = jArray.getJSONObject(i);
        r.add(json_data.getString("category"));
      }
      setListAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, r));
    }
    catch(JSONException e1){
      Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
    }
    catch (ParseException e1) {
      Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
    }
  }
}

错误

11-24 18:47:59.501: I/dalvikvm(1574): threadid=3: 对信号 3 做出反应
11-24 18:47:59.621: I/dalvikvm(1574): 将堆栈跟踪写入 '/data/anr/ traces.txt'
11-24 18:47:59.741: D/AndroidRuntime(1574): 关闭 VM
11-24 18:47:59.741: W/dalvikvm(1574): threadid=1: 线程以未捕获的异常退出(组= 0x409c01f8)
11-24 18:47:59.751: E/AndroidRuntime(1574): 致命异常: main
11-24 18:47:59.751: E/AndroidRuntime(1574): java.lang.RuntimeException: 无法启动活动 ComponentInfo {com.example.city/com.example.city.MainActivity}:java.lang.NullPointerException
11-24 18:47:59.751:E/AndroidRuntime(1574):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 1956)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 android. app.ActivityThread.access$600(ActivityThread.java:123)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-24 18 :47:59.751: E/AndroidRuntime(1574): 在 android.os.Handler.dispatchMessage(Handler.java:99)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 android.os.Looper。循环(Looper.java:137)11-24
18:47:59.751:E/AndroidRuntime(1574):在 android.app.ActivityThread.main(ActivityThread.java:4424)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 java.lang.reflect.Method.invokeNative(Native Method)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 java.lang .reflect.Method.invoke(Method.java:511)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-24 18:47:59.751: E/AndroidRuntime(1574) : 在 dalvik.system.NativeStart.main(Native Method)
11-24 18:47:59.751: E/AndroidRuntime(1574): 由: java.lang.NullPointerException
11-24 18:47:59.751: E/AndroidRuntime( 1574):在 org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 org.json.JSONTokener.nextValue(JSONTokener.java:94)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 org. json.JSONArray.(JSONArray.java:87)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 org.json.JSONArray.(JSONArray.java:103)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 com.example.city.MainActivity.onCreate(MainActivity.java:69)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 android.app.Activity.performCreate(Activity .java:4465)
11-24 18:47:59.751: E/AndroidRuntime(1574): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-24 18:47:59.751: E/AndroidRuntime(1574 ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-24 18:47:59.751: E/AndroidRuntime(1574): ... 11 更多
11-24 18:48:00.011: I/dalvikvm(1574): threadid=3: 对信号 3 做出反应
11-24 18: 48:00.011:I/dalvikvm(1574):将堆栈跟踪写入“/data/anr/traces.txt”11-24
18:48:00.291:I/dalvikvm(1574):threadid=3:对信号 3
11做出反应-24 18:48:00.321: I/dalvikvm(1574): 将堆栈跟踪写入“/data/anr/traces.txt”

4

2 回答 2

0

我已经回答了关于连接 php n mySQL 的问题,这里是链接 在这个你将看到如何从 php/mySQL 获取数据以登录 android。试试这个代码,然后你可以修改它以使用你想要的方式。

于 2012-11-24T15:47:13.287 回答
0

下面:

  JSONObject json_data=null;
  for(int i=0;i<jArray.length();i++ ){
    json_data = jArray.getJSONObject(i);
    r.add(json_data.getString("category"));
  }

在使用之前确保 json_data 有效。

于 2012-11-24T13:36:58.250 回答