2016-03-22 4 views
0

を使用します。ポストJsonObjectは、私は、バックエンドサーバーにデータを投稿するHttpPostを使用していますし、それは以下のコードでうまく動作バレーボール

 public void postData() { 

     String url = Configs.URL_OWS; 
     String odmName = Configs.ODM; 
     String keymd5 = getKeyMD5(); 

     JSONObject jsonObject = new JSONObject(); 
     jsonObject.put("model", model); 
     jsonObject.put("imei", imei1); 
     jsonObject.put("imei2", imei2); 
     jsonObject.put("build", buildVersion); 

     if (CommonUtils.isNetworkConnected(mContext)) { 
      // Create a new HttpClient and Post Header 
      Handler handler = new Handler(Looper.getMainLooper()); 

      HttpParams myParams = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(myParams, Configs.TIME_OUT); 
      HttpConnectionParams.setSoTimeout(myParams, Configs.TIME_OUT); 
      HttpClient httpclient = new DefaultHttpClient(myParams); 

      try { 
       HttpPost httppost = new HttpPost(url); 
       httppost.setHeader("Accept", "application/json"); 
       httppost.setHeader("Content-type", "application/json"); 
       httppost.addHeader("Authorization", odmName + ":" + keymd5); 
       // httppost.addHeader("POST", "/api/ows HTTP/1.1"); 

       StringEntity se = new StringEntity(jsonObject.toString()); 
       se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, 
         "application/json")); 

       httppost.setEntity(se); 
       HttpResponse response = httpclient.execute(httppost); 
       String result = EntityUtils.toString(response.getEntity()); 
       JSONObject jsonResponse = new JSONObject(result); 
       String status = jsonResponse.optString("status"); 

       if (status.equals(Configs.RESPONSE_OK)) { // response 200, send successfull 
        Log.i(Configs.APP_NAME + " " + TAG, "Send data successful"); 

       } else { 
        Log.i(Configs.APP_NAME + " " + TAG, "Send data failed:"); 
       } 
      } catch (final ClientProtocolException e) { 
       Log.i(Configs.APP_NAME + " " + TAG, "ClientProtocolException " + e.getMessage()); 
      } catch (final IOException e) { 
       Log.i(Configs.APP_NAME + " " + TAG, "IOException " + e.getMessage()); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.i(Configs.APP_NAME + " " + TAG, "Network not connected "); 
     } 
    } 

私の代わりにHttpPostボレーを使用するように切り替えるが、サーバーは常にエラーを返す、コードVolleyメソッドの場合:

public void postDataByVolley() { 

     String url = Configs.URL_OWS; 
     String odmName = Configs.ODM; 
     final String keymd5 = getKeyMD5(); 

     HashMap<String, String> params = new HashMap<String, String>(); 
     params.put("model", model); 
     params.put("imei", imei1); 
     params.put("imei2", imei2); 
     params.put("build", buildVersion); 


     if (CommonUtils.isNetworkConnected(mContext)) { 


      JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
        Request.Method.POST, 
        url, 
        new JSONObject(params), 

        new Response.Listener<JSONObject>() { 
         @Override 
         public void onResponse(JSONObject response) { 
          Log.i(Configs.APP_NAME + " " + TAG, "Success "); 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 
          Log.i(Configs.APP_NAME + " " + TAG, "Error: " + error.toString()); 

         } 
        }) { 

       @Override 
       public Map<String, String> getHeaders() throws AuthFailureError { 
        HashMap<String, String> headers = new HashMap<String, String>(); 
        headers.put("Accept", "application/json"); 
        headers.put("Content-type", "application/json"); 
        headers.put("Authorization", odmName + ":" + keymd5); 
        return headers; 
       } 
      }; 


      jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(Configs.TIME_OUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

      RequestQueue requestQueue = Volley.newRequestQueue(mContext); 
      requestQueue.add(jsonObjectRequest); 
     } 
    } 

Volleyメソッドのコードでどこが間違っているのかわかりません。私のVolley法に問題はありますか?

+0

エラーログを投稿する(存在する場合)。 – JUL2791

+0

@I_Droid:ログ:BasicNetwork.performRequest:予期しない応答コード400 – CauCuKien

+0

は、不正なリクエストを意味します。ヘッダーとパラメーターが正しく指定されていることを確認してください。 – JUL2791

答えて

0

あなたがそのURLをチェックしなさい、エラーに直面している、あなたはそれをよりよく

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/を理解するには次のリンクをチェックし、エラーを取得し、ここで をエラーログを貼り付け.onceパラメータが正しく送信されている理由とは言い難いです

+0

サーバーの応答:BasicNetwork.performRequest:予期しない応答コード400。 – CauCuKien

+0

ここでヘッダからコンテンツタイプjsonを削除し、それが動作するかどうかを確認してください.400は、通常、サーバAPIが期待するものが得られないことを意味します – VarunJoshi129

関連する問題