我正在使用凌空 post 数据到 device.I 通过 wifi.but 在应用程序中连接到它:NoConnectionError!无法连接到 192.168.4.1
I am using volley to post data to a device.I get connected to it by wifi.but in app: NoConnectionError! failed to connect to 192.168.4.1
这是我在我的应用程序 Java 中编写的代码:
我可以通过 python 代码发送数据,但在 android studio 中:failed to connect to 192.168.4.1
.
谁能给我一个建议?我真的需要这个来工作。
private void sendDeviceDataPostRequest() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://192.168.4.1:3333";
try {
String URL = url;
JSONObject jsonBody = new JSONObject();
//some data entry
JsonObjectRequest jsonOblect = new JsonObjectRequest(Request.Method.POST, URL, jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//code
} catch (JSONException e) {
//code
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//some code
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
//some code
}
};
queue.add(jsonOblect);
} catch (JSONException e) {
e.printStackTrace();
}
使用volley无法解决问题,最后使用TCP socket编程解决了。
这是代码:
class myTask 扩展 AsyncTask {
private static Socket s;
private static PrintWriter printWriter;
String send_message = "";
private static String ip = "192.168.4.1";
public static final int port = 3333;
public void set_data(String msg){
send_message = msg;
}
@Override
protected Void doInBackground(Void... voids) {
try {
s = new Socket(ip, port);
printWriter = new PrintWriter(s.getOutputStream());
printWriter.write(send_message);
printWriter.flush();
printWriter.close();
s.close();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
这是我在我的应用程序 Java 中编写的代码:
我可以通过 python 代码发送数据,但在 android studio 中:failed to connect to 192.168.4.1
.
谁能给我一个建议?我真的需要这个来工作。
private void sendDeviceDataPostRequest() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://192.168.4.1:3333";
try {
String URL = url;
JSONObject jsonBody = new JSONObject();
//some data entry
JsonObjectRequest jsonOblect = new JsonObjectRequest(Request.Method.POST, URL, jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//code
} catch (JSONException e) {
//code
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//some code
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
//some code
}
};
queue.add(jsonOblect);
} catch (JSONException e) {
e.printStackTrace();
}
使用volley无法解决问题,最后使用TCP socket编程解决了。
这是代码:
class myTask 扩展 AsyncTask
private static Socket s;
private static PrintWriter printWriter;
String send_message = "";
private static String ip = "192.168.4.1";
public static final int port = 3333;
public void set_data(String msg){
send_message = msg;
}
@Override
protected Void doInBackground(Void... voids) {
try {
s = new Socket(ip, port);
printWriter = new PrintWriter(s.getOutputStream());
printWriter.write(send_message);
printWriter.flush();
printWriter.close();
s.close();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}