2017-10-09 5 views
0

私はアンドロイドアプリでjsonデータを持っていて、ポート番号80の一部のIPアドレスに渡したいと思っています。下のコードでは、問題なくStringデータを送信できますが、jsonデータを渡そうとすると、私は エラー "インデックス22で、クエリ内の不正な文字を:http://192.168.x.x:80/ {" MainUi ":{" IPアドレス ":" 192.168.xxの」、 "メッセージ": "11月"、.....}アンドロイドから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上の任意のIPアドレスに文字列にデータを送信???? –

答えて

0

まず、UrlでJsonデータを送信するのは悪い習慣ですが、代わりに、HTTP POST要求の本文やHTTP GETリクエストのURLパラメータとしてデータを送信することをお勧めします。

文章安全でない文字(中括弧)をURLに入れます。

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

次のコードは、URLパラメータとしてポート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(); 
} 
関連する問題