简体   繁体   中英

Unable to connect to MySQL server/Android

I am new to Android. I want to connect to a MySQL server using PHP.

I have a deal database. In this database I have created a table category . This table consists of two columns. First is category_id , which has auto_increment and primary_key and havs data type INT. The second column is category , which has data type VARCHAR(30).

My PHP Script

<?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);
?>

My Android code:

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();
    }
  }
}

error

11-24 18:47:59.501: I/dalvikvm(1574): threadid=3: reacting to signal 3
11-24 18:47:59.621: I/dalvikvm(1574): Wrote stack traces to '/data/anr/traces.txt'
11-24 18:47:59.741: D/AndroidRuntime(1574): Shutting down VM
11-24 18:47:59.741: W/dalvikvm(1574): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-24 18:47:59.751: E/AndroidRuntime(1574): FATAL EXCEPTION: main
11-24 18:47:59.751: E/AndroidRuntime(1574): java.lang.RuntimeException: Unable to start activity 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(ActivityThread.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): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-24 18:47:59.751: E/AndroidRuntime(1574): at 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): Caused by: 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): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
11-24 18:47:59.751: E/AndroidRuntime(1574): at 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 more
11-24 18:48:00.011: I/dalvikvm(1574): threadid=3: reacting to signal 3
11-24 18:48:00.011: I/dalvikvm(1574): Wrote stack traces to '/data/anr/traces.txt'
11-24 18:48:00.291: I/dalvikvm(1574): threadid=3: reacting to signal 3
11-24 18:48:00.321: I/dalvikvm(1574): Wrote stack traces to '/data/anr/traces.txt'

Down here:

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

make sure that json_data is valid before you use it.

i have answered about connecting with php n mySQL , here is the link In this you will see how to get data from php/mySQL to login in android. try this code and then you can modify it to use how you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM