2012-04-05 6 views
0

Ich habe Probleme, wenn Sie versuchen, json auf dem Server von Android zu posten. Der Fehler lautet:So veröffentlichen Sie JSON String von Android

Fehler beim Laden von JSON. Sonderzeichen dürfen nicht in der Anfrage enthalten sein. Bitte überprüfen Sie den angeforderten JSON.

Ich habe viele Beispiele gefolgt, aber keine waren hilfreich. Bitte schlagen Sie eine Lösung vor oder helfen Sie mir, das Problem im Code zu finden.

Unten ist der Code zum Posten der JSON-Zeichenfolge auf dem Server.

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() { 

    } 

    public JSONObject getJSONFromUrl(String url) { 

     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      /* ====================================================================================================*/ 

         JSONObject listobj = new JSONObject(); 
         JSONObject listInvoice = new JSONObject(); 
         listInvoice.put("client_id",""); 
         listInvoice.put("date_from",""); 
         listInvoice.put("date_to",""); 
         listInvoice.put("invoice_number",""); 
         listInvoice.put("invoice_record_status",""); 
         listInvoice.put("invoice_status",""); 
         listInvoice.put("page","1"); 
         listInvoice.put("per_page_record","10"); 
         listobj.put("listInvoice", listInvoice); 


         //--List nameValuePairs = new ArrayList(1); 
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
         nameValuePairs.add(new BasicNameValuePair("json_data", listobj.toString())); 


         httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
         Log.d("JSON",listobj.toString()); 
      /*======================================================================================================*/ 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      String is = EntityUtils.toString(httpResponse.getEntity()); 

      Log.d("JSON","RESPONSE : " + is); 

      //--HttpEntity httpEntity = httpResponse.getEntity(); 
      //--is = httpEntity.getContent();   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     catch (JSONException e) { 
      Log.e("JSON",e.getMessage()); 
    } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

     } 
    } 
} 
+0

[Sonderzeichen in Json können das Problem pausieren] [1]. Versuchen Sie, kleine Sonderzeichen für Kleinbuchstaben zu verwenden. [1]: http://stackoverflow.com/questions/7866063/how-to-add-string-with-special-character-as-json-key – Snicolas

+1

Warum Sie iso-8859- verwenden 1 Codierung? Warum nicht UTF-8? – pepyakin

+0

@kknot ich auch mit utf-8 überprüft, aber immer noch mit demselben Problem –

Antwort

6

ist dies die Funktion json zu Ihrer URL

public void getServerData() throws JSONException, ClientProtocolException, IOException { 

    String title = Title_edittext.getText().toString().trim(); 
    String details = Details_edittext.getText().toString().trim(); 

    HttpParams httpParams = new BasicHttpParams(); 
    HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); 
    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 
    HttpClient client = new DefaultHttpClient(httpParams); 
    HttpPost request = new HttpPost(url); // add your url here... 

    request.setHeader("Content-Type", "application/json");   

    JSONObject json = new JSONObject();  
    json.put("client_id", channel_token); 
    json.put("date_from", data_src_id); 
    json.put("date_to", title); 

    Log.i("JSON Object", json.toString()); 

    StringEntity se = new StringEntity(json.toString()); 

    se.setContentEncoding("UTF-8"); 
    se.setContentType("application/json"); 

    request.setEntity(se);  

    HttpResponse response = client.execute(request); 

    HttpEntity entity = response.getEntity(); 
    InputStream is = entity.getContent(); 
    String _response = convertStreamToString(is); 
    System.out.println("res-- " + _response); 

    // Check if server response is valid code   
    int res_code = response.getStatusLine().getStatusCode(); 
    System.out.println("code-- " +res_code); 
} 
private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append((line + "\n")); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 
+0

rufen Sie diese Funktion mit URL, wenn Sie JSON übergeben möchten. –

0

Hier Link How to send a JSON object over Request with Android?

int TIMEOUT_MILLISEC = 10000; // = 10 seconds 
HttpParams httpParams = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); 
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 
HttpClient client = new DefaultHttpClient(httpParams); 

HttpPost request = new HttpPost(serverUrl); 
request.setEntity(new ByteArrayEntity(
    postMessage.toString().getBytes("UTF8"))); 
HttpResponse response = client.execute(request); 

Blick Linie dies für vorbei: request.setEntity (neu ByteArrayEntity ( postMessage.toString () .getBytes ("UTF8")));

Verwandte Themen