I'm very new to android programming and I have a problem with OutputStreamWriter. Now i'm working on simple test appication to send a POST requests to a server. There is a PHP application on the server side, which is designed to write data in a txt file. When i run my android app it goes through the code and the commands in the "finally" block are performed. The server really writes in the txt file, but it puts empty data.
I've tried also with GET method and tried to check REQUEST array. But no data are sent. If i delete from my code OutputStreamWriter I obtain the same picture, so it doesn't work at all. I also tried with using encoding, the same result.
I have done through similar questions: OutputStreamWriter not writing - writing to file and problem was with its creating;
Android, Java: HTTP POST Request - here and a few other questions is used httpClient which is depricated now.
HttpUrlConnection getOutputStream have plroblem, HttpUrlConnection getOutputStream have plroblem - here networking at main thread, but mine is run on the another one;
Android POST request not working, Java HttpURLConnection OutputStreamWriter is empty - is proposed to use reader to receive input stream. I've tried this solution, but the result was the same (no data sent)
My android version 6.0.1. What is the problem may be in?
package com.victoria.requester;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.*;
import java.net.*;
import android.widget.TextView;
import java.io.IOException;
import java.net.URL;
import java.net.HttpURLConnection;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "EXTRA_MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thread.start();
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
String url = "http://192.168.56.226:90/wfile.php";
String f_name = "123";
HttpURLConnection httpCon = null;
TextView textView = findViewById(R.id.textView);
textView.setText("OK1!");
try {
textView.setText("OK2!");
URL urlObj = new URL(url);
httpCon = (HttpURLConnection) urlObj.openConnection();
httpCon.setDoInput(true);
httpCon.setDoOutput(true);
//httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpCon.setRequestProperty("Content-Type", "text/html");
httpCon.setRequestMethod("POST");
String parameters = "f_name"+f_name;
//String parameters = URLEncoder.encode("f_name", "UTF-8") + "=" + URLEncoder.encode("123", "UTF-8");
httpCon.getResponseCode();
OutputStreamWriter writer = new OutputStreamWriter(httpCon.getOutputStream());
writer.write(parameters);
writer.close();
} catch (MalformedURLException e) {
textView.setText("bad URL!");
} catch (IOException e) {
textView.setText("network error!");
} finally {
httpCon.disconnect();
textView.setText("All done");
}
}
});
}