【问题标题】:How to retrieve data from mysql database for android application如何从 mysql 数据库中检索 android 应用程序的数据
【发布时间】:2014-03-30 05:20:42
【问题描述】:

我是 sakthi...我会从数据库中检索数据,但无法在 android 模拟器中显示它。如果我编辑我的代码,它会在文件停止工作时显示错误。我会在 json 中出错来处理数据。如果有人知道请帮助我...

public class Reportcard extends Activity
{
    @SuppressLint("NewApi")
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();


     @SuppressLint("NewApi")
    public void onCreate(Bundle savedInstanceState)
     {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.reportcard);
            StrictMode.setThreadPolicy(policy);             
                 String result = null;
                InputStream is = null;
                Bundle b=getIntent().getExtras();
                String id=(String)b.get("a");
                EditText e1=(EditText)findViewById(R.id.editText1);
                EditText e2=(EditText)findViewById(R.id.editText2);
                EditText e3=(EditText)findViewById(R.id.editText3);
                EditText e4=(EditText)findViewById(R.id.editText4);
                EditText e5=(EditText)findViewById(R.id.editText5);
                EditText e6=(EditText)findViewById(R.id.editText6);                
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("id",id));
                    try
                    {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://10.0.2.2/marks.php");
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost); 
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();
                        Log.e("log_tag", "connection success ");
                    }
                catch(Exception e)
                    {
                        Log.e("log_tag", "Error in http connection "+e.toString());
                        Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

                    }
                //convert response to string
                    try{
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),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());

                    Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();

                    }

                //parse json data
                try{


                        JSONObject object = new JSONObject(result);
                        String ch=object.getString("re");
                        if(ch.equals("success"))
                        {

                           JSONObject no = object.getJSONObject("0");
                           String a=no.getString("cg");
                        String w= no.getString("mpc");
                        String x=no.getString("dwdm");
                       // String my = NumberFormat.getInstance().format(x);
                        String e=no.getString("dsp");
                        String y=no.getString("tqm");
                        String z=no.getString("eefa");
                       //String myString = NumberFormat.getInstance().format(e);
                        e1.setText(a);
                        //tex1.setText(w);
                        e2.setText(w);
                        e3.setText(e);
                        e4.setText(x);
                        e5.setText(y);
                        e6.setText(z);
                        //e1.setText(z);

                          }


                        else
                        {

                            Toast.makeText(getApplicationContext(), "Record is not available.. Enter valid number", Toast.LENGTH_SHORT).show();

                        }


                }
                catch(JSONException e)
                {
                        Log.e("log_tag", "Error parsing data "+e.toString());
                        Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                }
          }
     }

这是我的 logcat 错误...

03-29 14:09:08.516: E/AndroidRuntime(1057): FATAL EXCEPTION: main
03-29 14:09:08.516: E/AndroidRuntime(1057): Process: com.example.hell, PID: 1057
03-29 14:09:08.516: E/AndroidRuntime(1057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hell/com.example.hell.Reportcard}: java.lang.NullPointerException
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.os.Looper.loop(Looper.java:137)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.app.ActivityThread.main(ActivityThread.java:4998)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at java.lang.reflect.Method.invoke(Method.java:515)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at dalvik.system.NativeStart.main(Native Method)
03-29 14:09:08.516: E/AndroidRuntime(1057): Caused by: java.lang.NullPointerException
03-29 14:09:08.516: E/AndroidRuntime(1057):     at com.example.hell.Reportcard.onCreate(Reportcard.java:107)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.app.Activity.performCreate(Activity.java:5243)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 14:09:08.516: E/AndroidRuntime(1057):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-29 14:09:08.516: E/AndroidRuntime(1057):     ... 11 more

【问题讨论】:

    标签: php android mysql json


    【解决方案1】:

    Android http 请求必须在 Handler 或 AsyncTask 中执行:

    查看我编写的课程:Java : a WebService asking embeded in a AsyncTask

    使用提供的示例。

    您的问题是您在实际获得服务器响应之前使用了response.getEntity();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 2015-12-03
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多