dtz88967
dtz88967
2012-10-24 13:18
浏览 80

Android通过php连接到mysql

I'm new to android and i would like to create an application that reads data from mysql server. I've taken from web an example about this, but i cannot make it work

I'm using eclipse for this and for the other part, php and mysql. First of all, here is my php script that is running ok (city.php):

<?php
//connecting to database
$sql=mysql_query("select * from city");
$output = array();
while(list($id,$name)=mysql_fetch_array($sql)){
    $output[$id]=$name;

}
print(json_encode($output));
mysql_free_result($sql);

?>

This is returning:

{"1":"Brasov","2":"Bucuresti"}

On my android project from eclipse, I have in the MainActivity.java:

package com.example.mycity;


import android.os.Bundle;
import android.app.Activity;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

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.net.ParseException;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity {

JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;

@Override
public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
     HttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost("http://www.steagu.ro/android/city.php");
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     HttpResponse response = httpclient.execute(httppost);
     if (response.getStatusLine().getStatusCode() != 200) {
            Log.d("MyApp", "Server encountered an error.");

     }
     HttpEntity entity = response.getEntity();
     is = entity.getContent();
     }catch(Exception e){
         //e.printStackTrace();
         Log.e("log_tag", "Error in http connection: "+e.toString());
    }

  }


}

But when I'm running this, I receive an error on logcat:it's not connecting to the file:Error in http connection: android.os.NetworkOnMainThreadException

10-24 13:04:34.429: I/dalvikvm(982): threadid=3: reacting to signal 3
10-24 13:04:34.649: I/dalvikvm(982): Wrote stack traces to '/data/anr/traces.txt'
10-24 13:04:34.859: E/log_tag(982): Error in http connection: android.os.NetworkOnMainThreadException
10-24 13:04:34.919: I/dalvikvm(982): threadid=3: reacting to signal 3
10-24 13:04:34.949: I/dalvikvm(982): Wrote stack traces to '/data/anr/traces.txt'
10-24 13:04:35.151: D/gralloc_goldfish(982): Emulator without GPU emulation detected.
10-24 13:04:35.479: I/dalvikvm(982): threadid=3: reacting to signal 3
10-24 13:04:35.499: I/dalvikvm(982): Wrote stack traces to '/data/anr/traces.txt'

I have permission for internet in the AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mycity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I don't know where is the problem. Can anybody help me with this? Thank you!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglianer5064
    donglianer5064 2012-10-24 13:59
    已采纳

    You may use an AsyncTask class to carry out your network operations as strictmode does not allow it to be done on the main UI example as follows

              public class MainActivity extends Activity {
    
             JSONArray jArray;
             String result = null;
             InputStream is = null;
              StringBuilder sb=null;
    
             @Override
            public void onCreate(Bundle savedInstanceState) {
    
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
           new httpRequest().execute();
    
            }
    
    
             private class httprRequest extends AsyncTask<String, Integer, String>{
    
                    @override
                    public String doInBackground(String.... params){
                       ArrayList<NameValuePair> nameValuePairs = new             ArrayList<NameValuePair>();
            //http post
              try{
               HttpClient httpclient = new DefaultHttpClient();
               HttpPost httppost = new HttpPost("http://www.steagu.ro/android/city.php");
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
               HttpResponse response = httpclient.execute(httppost);
               if (response.getStatusLine().getStatusCode() != 200) {
                   Log.d("MyApp", "Server encountered an error.");
    
                  }
              HttpEntity entity = response.getEntity();
              is = entity.getContent();
              }catch(Exception e){
              //e.printStackTrace();
              Log.e("log_tag", "Error in http connection: "+e.toString());
               }
    
                    }
                  }
    
               }
           }
    
    点赞 评论
  • douye2111
    douye2111 2012-10-24 13:20

    The problem is this:

    10-24 13:04:34.859: E/log_tag(982): Error in http connection: 
                        android.os.NetworkOnMainThreadException 
    

    You need to do your network access on a different thread.

    点赞 评论
  • drxpt06820
    drxpt06820 2012-10-25 07:37

    Thank you kabuto178, I'm made the changes and it's connecting. I've wrote the code also for retrieving data from mysql, so who need this, can use my example

    Here is the code:

    package com.example.mycity;
    
    
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    
    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.net.ParseException;
    import android.util.Log;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    String result = "";
    InputStream is = null;
    StringBuilder sb=null;
    //String ct_name = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new httprRequest().execute();
    
    
    int ct_id=0;
    String ct_name="";
    
    //Toast.makeText(getBaseContext(), "This is "+result, 3000).show();
    
    try {
    Thread.sleep(1000L);
    } catch (Exception te) {}
    
    try{
        //ct_name = result;
        JSONArray jArray = new JSONArray(result);
    
        for(int i=0;i<jArray.length();i++){
               JSONObject json_data= jArray.getJSONObject(i);
               ct_id=json_data.getInt("CITY_ID");
               ct_name += json_data.getString("CITY_NAME")+":";
           }
    
        }
        catch(JSONException e1){
          e1.printStackTrace();
        } catch (ParseException e1) {
                e1.printStackTrace();
        }
    
    TextView tv1 = (TextView) findViewById(R.id.textView1);
    tv1.setText(ct_name);
    
    }
    
    private class httprRequest extends AsyncTask<String, Integer, String>{
    
        @Override 
        public String doInBackground(String... params){
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    //http post
    try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.steagu.ro/android/city.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    if (response.getStatusLine().getStatusCode() != 200) {
       Log.d("MyApp", "Server encountered an error.");
    
      }
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    }catch(Exception e){
    //e.printStackTrace();
    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() + "
    ");
    
           String line="0";
           while ((line = reader.readLine()) != null) {
                          sb.append(line + "
    ");
            }
            is.close();
            result=sb.toString();
    }catch(Exception e){
            //Toast.makeText(getBaseContext(), "Am eroare aici", 3000).show();
            Log.e("log_tag", "Error converting result "+e.toString());
    }
    
    
    /*
    Toast.makeText(getBaseContext(), "Here i am", 3000).show();
    */
    
    return result;
        }
    
      }
    
    }
    

    I have to change a little bit the code, cause i put a thread.sleep. Instead of it, i have to check if i have a value into result (in a while loop). I think this is happening because at that moment when i try to parse the value, i don;t have a value in result.

    The php code is:

    <?php
    //connecting to database
    $sql=mysql_query("select * from city where CITY_NAME like '%'");
    $output = array();
    while($row=mysql_fetch_assoc($sql)){
        $output[]=$row;
    }
    
    echo json_encode($output);
    mysql_free_result($sql);
    
    ?>
    

    Table 'city' has two columns, CITY_ID and CITY_NAME.

    点赞 评论

相关推荐