2016-03-22 5 views
0

ich Httppost bin mit Daten zu einem Backend-Server zu veröffentlichen, und es weit unter diesem Code arbeiten:Beitrag JsonObject verwendet Volley

 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 "); 
     } 
    } 

Ich wechselte Volley zu verwenden, anstatt Httppost aber Server immer Fehler zurück, den Code für Volley-Methode:

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); 
     } 
    } 

Ich kann nicht finden, wo ich mit dem Code für Volley Methode falsch lag. Gibt es ein Problem in meiner Volley-Methode?

+0

Veröffentlichen Sie Ihr Fehlerprotokoll, falls vorhanden. – JUL2791

+0

@I_Droid: Protokoll: BasicNetwork.performRequest: Unerwarteter Antwortcode 400 – CauCuKien

+0

400 bedeutet ungültige Anforderung. Überprüfen Sie, ob Sie ordnungsgemäß mit Kopfzeilen und Parametern – JUL2791

Antwort

0

Es ist schwer zu sagen, warum Sie einen Fehler konfrontiert sind, dass die URL und Parameter richtig .Sobald Sie den Fehler dann log einfügen Fehler hier den folgenden Link überprüfen erhalten werden Überprüfen Sie bitte geschickt, es zu verstehen besser

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

+0

angefordert haben Serverantwort: BasicNetwork.performRequest: Unerwarteter Antwortcode 400. – CauCuKien

+0

Entfernen Sie einfach den Inhaltstyp json aus Ihrer Kopfzeile und überprüfen Sie, ob es funktioniert. 400 bedeutet normalerweise, dass die Server-API nicht das bekommt, was sie genau erwartet – VarunJoshi129