I want to send JSON from Android to the server using HTTP (POST).
But stop at the following line:
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
I use a thread for HTTP, and I added the following manifest:
<uses-permission android:name="android.permission.INTERNET" />
But the results did not change.
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private String PostUrl = "http://localhost:3000";
private String JSON = "{\"test\":\"100\"}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callPost();
}
public void callPost() {
handler.post(new Runnable() {
@Override
public void run() {
HttpURLConnection con = null;
try{
URL url = new URL(PostUrl);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type","application/JSON; charset=utf-8");
Log.d("debug", "----- All right here -----");
OutputStreamWriter out = new
OutputStreamWriter(con.getOutputStream());
Log.d("debug", "----- This message is not displayed -----");
out.write(JSON);
out.flush();
con.connect();
} catch (Exception e){
}
}
});
}
}
There is no error, and only "----- All right here -----" is displayed.