如何从android发送JSON数据到IP地址例如192.168.2.1:80?
问题描述:
我有一个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;
}
答
首先,在Url中发送Json数据是一种不好的做法,相反,作为一种良好的做法,您应该将数据发送到HTTP POST请求的主体中,或者作为HTTP GET请求中的url参数发送。
您的问题发生的原因是writi ng中的不安全字符(花括号)。
答
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();
}
答
谢谢大家,这是我如何执行程序。
下面的代码解释了我如何使用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();
}
JSON作为url参数???? –