我是Android新手。我想用PHP連接到MySQL服務器。無法連接到MySQL服務器/ Android
我有一個交易數據庫。在這個數據庫中,我創建了一個表category
。該表由兩列組成。首先是category_id
,它有auto_increment,primary_key和havs數據類型爲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);
?>
我的Android代碼:
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):主題ID = 3:對信號做出反應3
11-24 18:47:59.621:I/dalvikvm(1574):將堆棧跟蹤寫入'/data/anr/traces.txt'
11-24 18:47:59.741:D/AndroidRuntime(1574):關閉虛擬機
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:Unable開始活動ComponentInfo {com.example.city/com.example.city.MainActivity}:java.lang.NullPointerException
11-24 18:47:59.751:E/AndroidRuntime(1574):at android.app.ActivityThread。 performLaunchActivity(ActivityThread.java:1956)
11-24 18:47:59.751:E/AndroidRuntime(1574):at android.app.ActivityThread.handleLaunchActivity(ActivityT hread.java:1981)
11-24 18:47:59.751:E/AndroidRuntime(1574):at 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):at android.os.Looper.loop(Looper.java:137)
11-24 18:47: 59.751:E/AndroidRuntime(1574):at android.app.ActivityThread.main(ActivityThread.java:4424)
11-24 18:47:59.751:E/AndroidRuntime(1574):at java.lang.reflect.Method .invokeNative(Native Method)
11-24 18:47:59.751:E/AndroidRuntime(1574):at java.lang.reflect.Method.invoke(Method.java:511)
11-24 18:47:59.751:E/AndroidRuntime 1574):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
11-24 18:47:59.751:E/AndroidRuntime(1574):at com.android.internal.os .ZygoteInit.main(ZygoteInit.java:551)
11-24 18:47:59.751:E/AndroidRuntime(1574):at 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):at 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):at org.json.JSONArray。(JSONArray.java:103)
11-24 18:47:59.751:E/AndroidRuntime (1574):at com.example.city.MainActivity.onCreate(MainActivity.java:69)
11-24 18:47:59.751:E/AndroidRuntime(1574):at android.app.Activity.performCreate(Activity。 java:4465)
11-24 18:47:59.751:E/AndroidRuntime(1574):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-24 18:47:59.751:E/AndroidRuntime(1574):at 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作出反應將堆棧軌跡寫成'/ 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'