2017-10-09 45 views
0

我有一個JSON數據在Android應用程序,並希望傳遞給一些IP地址與端口號80.與代碼下面我能夠發送字符串數據沒有問題,但是當我嘗試傳遞JSON數據時,它開始給我錯誤 「在指數22查詢非法字符:http://192.168.x.x:80/ {」 MainUi 「:{」 Ip地址 「:」 192.168.xx的」, 「消息」: 「月」,...}如何從android發送JSON數據到IP地址例如192.168.2.1:80?

private class TaskRun extends AsyncTask<String, Void, String> { 

      String server; 

      TaskEsp(String server) { 
       this.server = server; 
      } 

      @Override 
      protected String doInBackground(String... params) { 
       String val = params[0]; 
       System.out.print(val); 
       final String p = "http://" + server + "/" + val; 

       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Log.v(TAG, p); 
        } 
       }); 


       String serverResponse = ""; 
       HttpClient httpclient = new DefaultHttpClient(); 
       try { 
        HttpGet httpGet = new HttpGet(); 
        httpGet.setURI(new URI(p)); 
        HttpResponse httpResponse = httpclient.execute(httpGet); 
        InputStream inputStream = null; 
        inputStream = httpResponse.getEntity().getContent(); 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
        serverResponse = bufferedReader.readLine(); 

        inputStream.close(); 
       } catch (URISyntaxException e) { 
        e.printStackTrace(); 
        serverResponse = e.getMessage(); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
        serverResponse = e.getMessage(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
        serverResponse = e.getMessage(); 
       } 

       return serverResponse; 
      } 
+0

JSON作爲url參數???? –

回答

0

首先,在Url中發送Json數據是一種不好的做法,相反,作爲一種良好的做法,您應該將數據發送到HTTP POST請求的主體中,或者作爲HTTP GET請求中的url參數發送。

您的問題發生的原因是writi ng中的不安全字符(花括號)。

0
JSONObject jsonObj = new JSONObject(); 
jsonObj.put("name", "Jhon"); 

HttpClient httpClient = new DefaultHttpClient(); 
      try { 
    HttpGet httpGet = new HttpGet(); 
    httpGet.setHeader("Content-type", "application/json"); 
    httpGet.setHeader("Accept-Encoding", "compress, gzip"); 
    httpGet.setHeader("Accept", "application/json"); 
    http.setURI(URI.create(p)); 

    StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8); 
    entity.setContentEncoding(HTTP.UTF_8); 
    entity.setContentType("application/json"); 

    HttpResponse httpResponse = httpClient.execute(httpGet); 

    InputStream inputStream = AndroidHttpClient.getUngzippedContent(httpResponse.getEntity()); 

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
    serverResponse = bufferedReader.readLine(); 

    inputStream.close(); 
} catch (URISyntaxException e) { 
    e.printStackTrace(); 
    serverResponse = e.getMessage(); 
} catch (ClientProtocolException e) { 
    e.printStackTrace(); 
    serverResponse = e.getMessage(); 
} catch (IOException e) { 
    e.printStackTrace(); 
    serverResponse = e.getMessage(); 
} 
0

謝謝大家,這是我如何執行程序。

下面的代碼解釋了我如何使用Json數據並將其作爲字符串發送。

public void writeJSON() { 
     JSONObject LedActivity = new JSONObject(); 
     JSONObject JsonRawData = new JSONObject(); 
     try { 
      LedActivity.put("Type of Light", Spinnerposition); //gets the current spinnerPosition 
      LedActivity.put("Light Brightness", Bit); 
      LedActivity.put("TimeDelay",TimePosition); //gets the seekbar position 
      LedActivity.put("Blink", isBlinkTurned); // gets the state of button 
      LedActivity.put("Fade", isFadeTurned); 

      JsonRawData.put("LedActivity", LedActivity); 
      jsonLight = JsonRawData.toString(1); 
      System.out.println(jsonLight); 
      sendPost(jsonLight); // send the json light string data to send post function 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

下面的代碼串中發送的數據的任何IP地址的端口80

public static void sendPost(final String postData) { 
    Thread thread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       URL url = new URL("http://" + IpSelected + ":80"); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); 
       conn.setRequestProperty("Accept", "application/json"); 
       conn.setDoOutput(true); 
       conn.setDoInput(true); 

       DataOutputStream os = new DataOutputStream(conn.getOutputStream()); 
       // os.writeBytes(URLEncoder.encode(postData, "UTF-8")); 
       os.writeBytes(postData); 

       os.flush(); 
       os.close(); 

       Log.i("STATUS", String.valueOf(conn.getResponseCode())); 
       Log.i("MSG", conn.getResponseMessage()); 

       conn.disconnect(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    thread.start(); 
} 
相關問題