2016-04-21 94 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