Android TextView与mysql的数据?

时间:2012-11-22 23:06:06

标签: java php mysql json

.HttpClient通过指定我的PHP文件的路径来发送命令来查询它似乎工作 。将接收到的流转换为字符串工作 但最终不起作用......我没有得到什么错误

我在纯Java projet中尝试导入所有需要json-lib等的包。它会对数据进行转换,但是在最后一步它失败了吗?!

服务器端:

<?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方:

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&gt;&gt;&gt;

    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.

1 个答案:

答案 0 :(得分:1)

此异常是由主线程上的网络操作引起的。 AsyncTask应该可以帮助您解决问题。

以下是几个可以帮助您开始使用AsyncTask的链接: