i make an app which accept xml data, but when i send query for this resource , looks that there is no connection . I added android internet permissions and have net in browser but in my app there isn't any connection.
this is the code
protected String sendRequest(String urlAdr,ArrayList postVars){
String data=extractPairValuesToString(postVars);
urlAdr+=data; //send all variables in the url not from request properties
String xmlResponse=null;
HttpURLConnection con = null;
URL url;
try {
url = new URL(urlAdr);
con = (HttpURLConnection) url.openConnection();
//con.setReadTimeout(10000 /* milliseconds */);
//con.setConnectTimeout(15000 /* milliseconds */);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Length", ""+Integer.toString(data.getBytes().length));
con.setDoInput(true);
con.setDoOutput(true);
}catch (IOException e) {
setErrorStatus(e.getMessage());
}
}
More times than not, the manifest is missing:
<uses-permission android:name="android.permission.INTERNET"/>
See here:
http://developer.android.com/resources/tutorials/views/hello-webview.html
Related
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept", "application/json");
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(data.toString());
wr.flush();
status = con.getResponseCode();
This code is part of an AsyncTask class in Android Studio. When I execute a Post request using PostMan, the status returned is 200; however, when I make the request using the android emulator, the status is returned as 0. I'm super confused, and some help would be appreciated.
Status code 0 means some kind of network error.
Try checking for:
Internet permission
Emulator, Connectivity
I have a problem with facebook.
In connection i have:
URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token="+ token);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int sc = con.getResponseCode();
and I get:
java.net.UnknownHostException: Unable to resolve host "www.googleapis.com": No address associated with hostname
I have all permissions in manifest like:access_network and Internet.
I resolve my problem:
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000 /* milliseconds */);
con.setConnectTimeout(15000 /* milliseconds */);
con.setRequestMethod("GET");
con.setDoInput(true);
// Starts the query
con.connect();
int sc = con.getResponseCode();
So I am currently writing an android app that will get tweets from a particular user, using Twitter's 1.1 Application-only-auth, which requires a POST HTTP request to get a bearer token. I have a method set up that uses an HttpsURLConnection to open a connection, but
conn.setDoOutput(true); method isn't working
conn.doOutput stays false
the request stays the default GET.
Am I doing something wrong?
This method is called in an AsyncTask's doInBackground() method:
public String getRequestBearerToken(String endUrl) throws IOException {
String encodedCred = encodeKeys("API-key", "API-secret");
HttpsURLConnection connection = null;
try {
URL url = new URL(endUrl);
connection = (HttpsURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
// POST request properties
connection.setRequestProperty("Host", "api.twitter.com");
connection.setRequestProperty("User-Agent", getResources()
.getString(R.string.app_name));
connection.setRequestProperty("Authorization", "Basic "
+ encodedCred);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
connection.setRequestProperty("Content-Length", "29");
connection.setUseCaches(false);
...
Found couple of links here and there.. But none of them worked for me.
Trying to achieve is the following :
1) I have a server specified in location server address (e.g : www.google.com:8080)
2) The server has a php script to handle the input.
3) Wanted to create a text file in the server at location /var/www/working/
4) The content is stored in a string urlFile (which is a structured json)
5) Writing the string directly to the server location rather than messing user's device
private class WebServiceCall extends AsyncTask<String, Void, String>{
protected String doInBackground(String... urls) {
try {
URL url = new URL(locationServer + "/handle.php");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// Setting chunk improves performance
conn.setChunkedStreamingMode(0);
//Enabling POST method
((HttpURLConnection) conn).setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
FileOutputStream serverWriter = new FileOutputStream(new File("/var/www/working/jsonFile.txt"), false);
serverWriter.write(urlFile.getBytes());
serverWriter.close();
conn.disconnect();
}
catch(IOException e){
e.printStackTrace();
return "Server Refused";
}
return "Uploaded";
}
This is my code snippet, would appreciate any help.
Thanks!
Suman
I need help with sending http get request. Like this:
URL connectURL;
connectURL = new URL(address);
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
// do some setup
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
// connect and flush the request out
conn.connect();
conn.getOutputStream().flush();
// now fetch the results
String response = getResponse(conn);
et.setText(response);
I searched the web but any method I try, the code fails at 'conn.connect();'. Any clues?
Very hard to tell without the actual error message. Random thought: did you add the internet permission to you manifest?
<uses-permission android:name="android.permission.INTERNET"/>
If you want some demo code then try following:
URL url = new URL("url.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
and this:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
writeStream(out);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
Hope this helps.