从mysql服务器检索JSON数据并将其存储到android中的对象

时间:2016-12-05 09:00:19

标签: android mysql json android-volley store

我是android和mySQL服务器的新手。我正在编写一个应该从服务器检索JSON数据的应用程序,然后将数据存储到对象“Court”。我正在使用Volley库。但是当我尝试存储它并在“onRespons”方法之外使用它时,我得到的对象或String为null。

这是我到目前为止所尝试的内容。

DatabaseConnection.java UPDATED !!!

public class DataBaseConnection extends AppCompatActivity {


String temp;
Court court;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        court = new Court();
    }

public String retrive(final Context context, final String newURL) {
    final RequestQueue request;
    request = newRequestQueue(context);



    Response.Listener<String> sendR = new Response.Listener<String>() {
        //Court[] courts;







        @Override
        public void onResponse(String response) {



            sharedResponse(response);

            court.setName(response);




        }



    };

    System.out.println(court.getName());

    Response.ErrorListener errorr = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("There is an error");
            System.out.println(error.getMessage());
        }
    };

    StringRequest sender = new StringRequest(Request.Method.POST, newURL,sendR,errorr) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("test", "test");

            return parameters;

        }
    };



    request.add(sender);
    return court.getName();
}


private void sharedResponse(String response) {
    SharedPreferences m = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = m.edit();
    editor.putString("Response", response);
    editor.commit();
}
}

这就是升级班:

public class Listing extends AppCompatActivity {

private  String url = "http://192.168.1.106/list/list.php";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listing);
    String JSON="";


    DataBaseConnection connect = new DataBaseConnection();
    JSON = connect.retrive(getApplicationContext(), url);

    System.out.println("first time"+JSON);

    SharedPreferences m = PreferenceManager.getDefaultSharedPreferences(this);
    JSON = m.getString("Response", "");

    System.out.println("Second time"+JSON);

}
}

如您所见,我尝试使用简单的返回值和SharedPrefrences,我也尝试使用Intent。但他们都没有和我合作。从Log Cat开始,显示有关NULLPointerException的一些错误。

LogCat:

    12-05 11:45:40.815 30821-30821/com.saihatandroid.owen.lising I/System.out: first time
12-05 11:45:40.817 30821-30821/com.saihatandroid.owen.lising I/System.out: Second time
12-05 11:45:40.865 30821-30852/com.saihatandroid.owen.lising I/Adreno: QUALCOMM build                   : 4d8478c, I7b6ba47bd6
                                                                       Build Date                       : 02/09/16
                                                                       OpenGL ES Shader Compiler Version: XE031.07.00.01
                                                                       Local Branch                     : mybranch18178856
                                                                       Remote Branch                    : quic/LA.HB.1.3.1_rb1.11
                                                                       Remote Branch                    : NONE
                                                                       Reconstruct Branch               : NOTHING

12-05 11:45:41.025 30821-30821/com.saihatandroid.owen.lising E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.saihatandroid.owen.lising, PID: 30821
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                                                                                   at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
                                                                                   at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375)
                                                                                   at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370)
                                                                                   at com.saihatandroid.owen.lising.DataBaseConnection.sharedResponse(DataBaseConnection.java:148)
                                                                                   at com.saihatandroid.owen.lising.DataBaseConnection.access$000(DataBaseConnection.java:44)
                                                                                   at com.saihatandroid.owen.lising.DataBaseConnection$1.onResponse(DataBaseConnection.java:75)
                                                                                   at com.saihatandroid.owen.lising.DataBaseConnection$1.onResponse(DataBaseConnection.java:61)
                                                                                   at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
                                                                                   at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                                                   at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:168)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5885)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:819)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:709)

同样发现此错误在Logcat的底部

D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:113 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

我需要你的帮助我在网络上找不到任何与之相关的内容,我尝试了所有选项。 谢谢

0 个答案:

没有答案