Reputation: 71
I have a problem and really do not know what is wrong :-/
I have a simple function called after button click
public void clickFunc(View view) {
StringBuilder result = new StringBuilder();
try {
URL url = new URL("http://google.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
in.close();
httpTxt.setText(result.toString());
}
else {
httpTxt.setText("false : "+responseCode);
}
}
catch(Exception e){
httpTxt.setText("Exception: " + e.getMessage());
}
}
But after a click my output is Exception: null
In manifest I have <uses-permission android:name="android.permission.INTERNET" />
This code is by my opinion same as all codes mentioned here or at internet :-/
httptxt is my texview for output from the page
Thank you
Upvotes: 0
Views: 804
Reputation: 2668
There are many threads on android, When are you hit an http request this mean it will work with another Thread,So if do you want to hit an http request you have to put it under async class because it's a long operation, and finally if do you want to change by http response
you have to put your changes inside UI thread which mean it should be under Handler
or RunOnUiThread
because Async task can't have access to UI thread directly
if you don't want to use Asynctask and runUiThred neither handler
use volley it's google's API allowing
Upvotes: 0
Reputation: 71
Ok, I did not know I have to do it in the background... it is working now,thank you!
public void clickFunc(View view) {
new SendPostRequest().execute();
}
private class SendPostRequest extends AsyncTask<Void, Void, String> {
protected String doInBackground (Void... params) {
StringBuilder result = new StringBuilder();
HttpURLConnection conn = null;
BufferedReader reader;
try {
URL url = new URL("http://stackoverflow.com");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(conn.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
in.close();
return(result.toString());
}
else {
return("false : "+responseCode);
}
}
catch(Exception e){
return("Exception: " + e.getMessage());
}
}
protected void onPostExecute(String s) {
super.onPostExecute(s);
httpTxt.setText(s);
}
}
I am not an android programmer, so even if its basic I did not know that, sorry guys :)
About my app if you are interested why I wanted it - I read a barcode, go to the php script, look into the database if there is this code and return true or false
Upvotes: 2