2016-04-21 60 views
0

我尝试开发一个服务器/客户端应用程序。服务器是glassfish,客户端android(Google http api)。Http Put Json Android

我使用休息来传输数据。

如果我做了一个Get all很好。

现在我想做一个看跌的,但我没有得到我的服务器我的内容...

客户:

JSONObject jO = new JSONObject(json); 
final HttpContent content = new JsonHttpContent(new JacksonFactory(), jO); 
HttpRequest request = requestFactory.buildPutRequest(url, content); 

服务器:

@PUT 
@Consumes(MediaType.APPLICATION_JSON) 
public JsonObject putJson(JsonObject jO, @Context HttpHeaders headers){ 

有了休息的调试器(邮差),我可以发送一些JSON到服务器并得到它。 使用android JsonObject进行放置(android上的内容调试为填充)。

你能帮我吗?

更新与凌空

服务器:

/** 
* PUT method for updating or creating an instance of Registration 
* 
* @param param1 
* @param jO 
* @param headers 
* @return 
*/ 
@POST 
@Consumes("application/json") 
public JsonObject putJson(@QueryParam("email") String param1, @Context HttpHeaders headers){ 

    Player tmpPlayer; 
    System.out.println(param1); 
JsonObject value = Json.createObjectBuilder() 
.add("firstName", "John") 
.add("lastName", "Smith").build(); 

    return value; 

客户端:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, 
      URL_REGISTER, null, 
      new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        System.out.println(response.toString()); 
       } 
      }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      System.out.println("Error"); 
     } 
    }) { 

     @Override 
     public String getBodyContentType() { 
      return "application/json"; 
     } 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      headers.put("Content-Type", "application/json"); 
      return headers; 
     } 

     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 

      JSONObject tmpObject = new JSONObject(); 
      try { 
       tmpObject.put("name", "simon"); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      params.put("name", tmpObject.toString()); 
      params.put("email", "[email protected]"); 
      params.put("password", "1234"); 

      return params; 
     } 

    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(registration); 
    requestQueue.add(request); 
    requestQueue.start(); 

感谢西蒙

+0

你在正确的内容类型HTTP头说明您的内容填充? (在buildPutRequest()方法中的某处) –

+0

用request.getHeaders()。setContentType(“application/json”);同样的问题 – user3657241

+0

你想使用凌空 –

回答

0

我recomended您使用排(也是谷歌API)

String url = "http://" + URL "; 
    progressBar.setVisibility(View.VISIBLE); // in case you have progressBar 
    StringRequest request = new StringRequest(Request.Method.PUT, url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      progressBar.setVisibility(View.INVISIBLE); 
      JSONObject jsonObject = null; 
      user = null; 
      try { 
       jsonObject = new JSONObject(response); 
       //do something usefull 

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



     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e("volley error", error.toString()); 


     } 
    }) { 
     @Override 
     public String getBodyContentType() { //override 
// the content type because the request is string not JSON 
// request note that if you used JSON request it will don't work with rest 

      return "application/json"; 
     } 
//override getParams so that the server can recieve the paramters otherwise 
// paramters will be null at server 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      Map<String, String> parameters = new HashMap<String, String>(); 
      parameters.put("param1", myText.getText().toString()); 
      parameters.put("param2", myText.getText().toString()); 


      return parameters; 
     } 
    }; 

    requestQueue.add(request); 
+0

嗨,感谢您的重播。我尝试它,但我没有得到它:-(我如何发送json?我想在getParams它的唯一映射与字符串/字符串在我的服务器上我没有得到我要发送的json如果我说在我的服务器我等待对于字符串的字符串也是空的...我怎么能得到我的服务器上的东西 – user3657241

+0

在这个请求你的web服务应该接收json并返回字符串作为json结构为什么你没有得到它吗? Gradle? –

+0

嗨,是我的服务器上的凌空工作断点,但params ar null给我一分钟我会更新我的话题现在有什么 – user3657241

0

您可以使用HttpURLConnection来完成这项工作。

  URL url = new URL("http://www.example.com/someAPI.php"); 

      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setDoOutput(true); 
      // when you are PUT do make sure you assign appropriate header 
      // In this case POST. 

      httpURLConnection.setRequestMethod("PUT"); 
      httpURLConnection.connect(); 

      // like this you can create your JOSN object which you want to PUT what every JON body you like to PUT 
      JsonObject jsonObject = new JsonObject(); 
      jsonObject.addProperty("write", "whatever"); 
      jsonObject.addProperty("you", "want"); 

      // And this is how you will write to the URL 
      DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); 
      wr.writeBytes(jsonObject.toString()); 
      wr.flush(); 
      wr.close(); 

OkHttp

这提供了GETPUTPOST更优雅的方式,DELETE

GET 

    OkHttpClient okHttpClient = new OkHttpClient(); 
    String url_String = "API url"; 
    Request request = new Request.Builder() 
      .url(url_String) 
      .get() 
      .build(); 
    okHttpClient.newCall(request).enqueue(new Callback() { 
     @Override 
     public void onFailure(Call call, IOException e) { 
      Log.d(TAG, "onFailure: "); 
     } 

     @Override 
     public void onResponse(Call call, Response response) throws IOException { 
      String response_String = response.body().string(); 
      Log.d(TAG, "onResponse: \n"+response_String); 
     } 
    }); 

PUT 
    OkHttpClient okHttpClient = new OkHttpClient(); 
    String url_String = "API url"; 
    MediaType JSON 
      = MediaType.parse("application/json; charset=utf-8"); 
    RequestBody body = RequestBody.create(JSON, "JSON body to be PUT"); 
    Request request = new Request.Builder() 
      .url(url_String) 
      .put(body) 
      .build(); 
    okHttpClient.newCall(request).enqueue(new Callback() { 
     @Override 
     public void onFailure(Call call, IOException e) { 
      Log.d(TAG, "onFailure: "); 
     } 

     @Override 
     public void onResponse(Call call, Response response) throws IOException { 
      String response_String = response.body().string(); 
      Log.d(TAG, "onResponse: \n"+response_String); 
     } 
    }); 

Post会像PUT一样工作,你只需要通过post来替换put方法。

0

变化@Query参数去@form PARAM

+0

得到一个错误becaus json所以我改变它的应用程序/ x-www-form-urlencoded现在我可以再次与邮递员工作,但android仍然不工作param1 = null ...但方法getParams永远不会达到为什么?方法getHeaders是工作 – user3657241

+0

他是beacus我hav使用jsonrequest而不是stringrequest ....但与stringrequst我现在得到意外的响应代码400 – user3657241

+0

得到它感谢您的帮助,我努力 – user3657241