简体   繁体   中英

Android TextView with Data from mysql?

.HttpClient sending commands by specifying the path of my PHP file to query this on seems to work .Converting the received stream into string work too But the end does not work ... I don't get what's wrong

I try the same thing in a pure Java projet importing all packages needed json-lib etc.. it does the conversion of the data but on the last step it fail ?!

Server side:

<?php
    $Conn = mysql_connect("127.0.0.1","root","socrates");
    if (!$Conn)
        echo "not connected";
    $DbSelect = mysql_select_db("impetodb", $Conn);
    if (!$DbSelect)
    echo "db non connecté";
    $sql=mysql_query("select * from patients_infos ");

    while ($row=mysql_fetch_assoc($sql)) $output[]=$row;
    print (json_encode($output));

 mysql_close();

?>

ANDROID App side:

package com.example.getdata3;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;


public class ConnexionSQLActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
   try 
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_connexion_sql);
       TextView myTextView = (TextView)findViewById(R.id.patientName);
       String result = "";


    try {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/index.php");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();

        try
          {
           BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
           StringBuilder sb  = new StringBuilder();
           String line = null;

           while ((line = reader.readLine()) != null){

               sb.append(line + "\n");
           }

           is.close();

           result = sb.toString();


          }catch (Exception e){
              Log.e("log_tag","Error converting result"+e.toString());
          }


    }catch (Exception e){
        Log.e("log_tag","Error in Http Connexion"+e.toString());
    }    


    try {
            JSONArray jArray = new JSONArray(result);

             for(int i=0; i<jArray.length(); i++)
             {
               JSONObject json_data = jArray.getJSONObject(i);
               myTextView.setText(json_data.getString("nom"));
               //r.add(json_data.getString("categorie"));   
               }

    } catch (Exception e) {
        Log.e("log_tag","Error parsing data"+e.toString());
    }


    }catch (Exception e){

    Log.e("ERROR","ERROR IN CODE"+e.toString());

   }    

}

}

Logcat >>>

    11-23 12:53:51.325: W/System.err(551): android.os.NetworkOnMainThreadException
11-23 12:53:51.335: W/System.err(551):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
11-23 12:53:51.335: W/System.err(551):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:74)
11-23 12:53:51.335: W/System.err(551):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-23 12:53:51.335: W/System.err(551):  at libcore.io.IoBridge.connect(IoBridge.java:112)
11-23 12:53:51.335: W/System.err(551):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-23 12:53:51.335: W/System.err(551):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-23 12:53:51.335: W/System.err(551):  at java.net.Socket.connect(Socket.java:842)
11-23 12:53:51.335: W/System.err(551):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
11-23 12:53:51.346: W/System.err(551):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
11-23 12:53:51.346: W/System.err(551):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-23 12:53:51.346: W/System.err(551):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-23 12:53:51.346: W/System.err(551):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-23 12:53:51.346: W/System.err(551):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-23 12:53:51.346: W/System.err(551):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-23 12:53:51.346: W/System.err(551):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-23 12:53:51.346: W/System.err(551):  at com.example.getdata3.ConnexionSQLActivity.onCreate(ConnexionSQLActivity.java:37)
11-23 12:53:51.346: W/System.err(551):  at android.app.Activity.performCreate(Activity.java:4465)
11-23 12:53:51.346: W/System.err(551):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-23 12:53:51.355: W/System.err(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-23 12:53:51.355: W/System.err(551):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-23 12:53:51.355: W/System.err(551):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-23 12:53:51.355: W/System.err(551):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-23 12:53:51.355: W/System.err(551):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 12:53:51.355: W/System.err(551):  at android.os.Looper.loop(Looper.java:137)
11-23 12:53:51.355: W/System.err(551):  at android.app.ActivityThread.main(ActivityThread.java:4340)
11-23 12:53:51.355: W/System.err(551):  at java.lang.reflect.Method.invokeNative(Native Method)
11-23 12:53:51.365: W/System.err(551):  at java.lang.reflect.Method.invoke(Method.java:511)
11-23 12:53:51.365: W/System.err(551):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-23 12:53:51.365: W/System.err(551):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-23 12:53:51.365: W/System.err(551):  at dalvik.system.NativeStart.main(Native Method)
11-23 12:53:51.375: E/log_tag(551): Error converting resultandroid.os.NetworkOnMainThreadException
11-23 12:53:51.375: W/System.err(551): org.json.JSONException: End of input at character 0 of 
11-23 12:53:51.385: W/System.err(551):  at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
11-23 12:53:51.385: W/System.err(551):  at org.json.JSONTokener.nextValue(JSONTokener.java:97)
11-23 12:53:51.395: W/System.err(551):  at org.json.JSONArray.<init>(JSONArray.java:87)
11-23 12:53:51.395: W/System.err(551):  at org.json.JSONArray.<init>(JSONArray.java:103)
11-23 12:53:51.395: W/System.err(551):  at com.example.getdata3.ConnexionSQLActivity.onCreate(ConnexionSQLActivity.java:70)
11-23 12:53:51.405: W/System.err(551):  at android.app.Activity.performCreate(Activity.java:4465)
11-23 12:53:51.405: W/System.err(551):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-23 12:53:51.405: W/System.err(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-23 12:53:51.405: W/System.err(551):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-23 12:53:51.415: W/System.err(551):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-23 12:53:51.415: W/System.err(551):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-23 12:53:51.415: W/System.err(551):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 12:53:51.415: W/System.err(551):  at android.os.Looper.loop(Looper.java:137)
11-23 12:53:51.415: W/System.err(551):  at android.app.ActivityThread.main(ActivityThread.java:4340)
11-23 12:53:51.415: W/System.err(551):  at java.lang.reflect.Method.invokeNative(Native Method)
11-23 12:53:51.415: W/System.err(551):  at java.lang.reflect.Method.invoke(Method.java:511)
11-23 12:53:51.415: W/System.err(551):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-23 12:53:51.425: W/System.err(551):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-23 12:53:51.425: W/System.err(551):  at dalvik.system.NativeStart.main(Native Method)
11-23 12:53:51.425: E/log_tag(551): Error parsing dataorg.json.JSONException: End of input at character 0 of 
11-23 12:53:51.575: D/gralloc_goldfish(551): Emulator without GPU emulation detected.

This exception was caused by doing network operation on main thread. AsyncTask should help you fix the problem.

Here are several link that should get you started with AsyncTask:

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