无法使用JSON Android动态上传字段

时间:2014-10-11 10:12:01

标签: android json parsing

代码:

    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override

        //http://********.***?user_id=11&address=Sanvordem%20-%20Dabal%20-%20Dharbandoda%20Road,%20Sanvordem,%20Goa%20403706,%20India&latitude=15.274322762603436&langtitude=74.11858866662601&property_id=Earth%20House&room_type=Shared%20Room&bedrooms=1&title=rttrrtrt&description=rtrtrtrtrtrtrt&capacity=Shared%20Room&price=45&currency=USD&phone=3453535&amenities=Pool,Heating&cancellation_policy=Strict
        public void onClick(View v) {
            String user="111";
            String prpoer="Earth House";
            String privicy="Shared Room";
            String beds="7";
            String tit="timothy";
            String descp="description test";
            String Accommodat="12";
            String pricerp="12";
            String priceusd="USD";
            String Cancelation="Strict";
            String phonenum="3434343434";


            //finalString url2 = "http://********************.***?user_id=111&address=Sanvordem%20-%20Dabal%20-%20Dharbandoda%20Road,%20Sanvordem,%20Goa%20403706,%20India&latitude=15.274322762603436&langtitude=74.11858866662601&property_id="+prpoer+"&room_type="+privicy+"&bedrooms="+beds+"&title="+tit+"&description="+descp+"&capacity="+Accommodat+"&price="+pricerp+"&currency="+priceusd+"&phone="+phonenum+"&amenities=Pool,Heating&cancellation_policy="+Cancelation;
             //String url=url2.toString();
            //Log.d("URL IS:",url);

             String url="http://********************.***?user_id=111&address=Sanvordem%20-%20Dabal%20-%20Dharbandoda%20Road,%20Sanvordem,%20Goa%20403706,%20India&latitude=15.274322762603436&langtitude=74.11858866662601&property_id=Earth%20House&room_type=Shared%20Room&bedrooms=7&title=timothy&description=description%20test&capacity=12&price=12&currency=USD&phone=3434343434&amenities=Pool,Heating&cancellation_policy=Strict";
             // Creating volley request obj
            JsonArrayRequest movieReq = new JsonArrayRequest(url,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            Log.d(TAG, response.toString());


                            // Parsing json
                            for (int i = 0; i < response.length(); i++) {
                                try {

                                    JSONObject obj = response.getJSONObject(i);
                                    String resu= obj.getString("reason_message"); 
                                    Log.d("OUTPUT IS",resu);
                                    if(resu.matches("List added successfully."))
                                    {
                                        AlertDialog alertDialog = new AlertDialog.Builder(listyourspace.this).create();
                                        alertDialog.setTitle("Data");
                                        alertDialog.setMessage("List added successfully....");
                                        alertDialog.show();
                                        Log.d("Success","success");
                                    }



                                    // adding movie to movies array


                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }

                            // notifying list adapter about data changes
                            // so that it renders the list view with updated data
                            //adapter.notifyDataSetChanged();
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d(TAG, "Error: " + error.getMessage());


                        }
                    });

            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(movieReq);
          }

    });
}

Logcat输出:

10-08 12:31:47.026: D/URL IS:(28292): http://********************.***?user_id=111&address=Sanvordem%20-%20Dabal%20-%20Dharbandoda%20Road,%20Sanvordem,%20Goa%20403706,%20India&latitude=15.274322762603436&langtitude=74.11858866662601&property_id=Earth House&room_type=Shared Room&bedrooms=7&title=timothy&description=description test&capacity=12&price=12¤cy=USD&phone=3434343434&amenities=Pool,Heating&cancellation_policy=Strict
10-08 12:31:48.470: E/Volley(28292): [26752] BasicNetwork.performRequest: Unexpected response code 500 for http://********************.***?user_id=111&address=Sanvordem%20-%20Dabal%20-%20Dharbandoda%20Road,%20Sanvordem,%20Goa%20403706,%20India&latitude=15.274322762603436&langtitude=74.11858866662601&property_id=Earth House&room_type=Shared Room&bedrooms=7&title=timothy&description=description test&capacity=12&price=12¤cy=USD&phone=3434343434&amenities=Pool,Heating&cancellation_policy=Strict
10-08 12:31:48.476: D/AndroidRuntime(28292): Shutting down VM
10-08 12:31:48.479: E/AndroidRuntime(28292): FATAL EXCEPTION: main
10-08 12:31:48.479: E/AndroidRuntime(28292): Process: info.androidhive.customlistviewvolley, PID: 28292
10-08 12:31:48.479: E/AndroidRuntime(28292): java.lang.NullPointerException: format == null
10-08 12:31:48.479: E/AndroidRuntime(28292):    at java.lang.String.format(String.java:1960)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at com.android.volley.VolleyLog.buildMessage(VolleyLog.java:79)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at com.android.volley.VolleyLog.d(VolleyLog.java:55)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at info.androidhive.customlistviewvolley.listyourspace$6$2.onErrorResponse(listyourspace.java:404)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at com.android.volley.Request.deliverError(Request.java:563)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at android.os.Handler.handleCallback(Handler.java:733)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at android.os.Handler.dispatchMessage(Handler.java:95)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at android.os.Looper.loop(Looper.java:136)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at android.app.ActivityThread.main(ActivityThread.java:5086)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at java.lang.reflect.Method.invoke(Native Method)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-08 12:31:48.479: E/AndroidRuntime(28292):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

1 个答案:

答案 0 :(得分:0)

如果您正在获取http状态代码(500),那么凌空按预期工作,您可能会发送服务器无法处理的请求。

我建议您尝试使用已知可用的公共API。我建议打开天气图。

http://openweathermap.org/api

由于