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
Related
I found an issue in Samsung S5 with using HTTPURLCONNECTION may be I'm doing wrong but if any one have an idea kindly guide me.
Here is my code :
URL urls = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urls.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("x-os-application-token", Constants.APPLICATION_TOKEN);
connection.setConnectTimeout(900000);
connection.setReadTimeout(150000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(json);
wr.flush();
wr.close();
int responseCode = connection.getResponseCode();
Now using this code if I manually Internet disconnect (WI_FI OFF) then other device (e.g. Nexus 5 and Samsung Galaxy Note 3) give me response immediate but In Samsung S5 it respond me after connection.setReadTimeout(150000) ??? 2.5 minutes
I have responseCode = 403.
Do you see any issue in the code below?
I tried every flag in the Base64.encode.
My site woks perfectly fine: once I login in manually from http passing my username/passwd, I'm able to retrieve the JSON data using https.
Do you think I'm correct in suspecting an authorization issue?
URL object = new URL("https://aaaaaa.bb/api/?format=json" );
HttpURLConnection connection = (HttpURLConnection) object.openConnection();
connection.setReadTimeout(60 * 1000);
connection.setConnectTimeout(60 * 1000);
connection.setRequestMethod("GET");
String authorization="xxxxxx#gmail.com:yyyyyy";
String encodedAuth="Basic "+ Base64.encode(authorization.getBytes(), Base64.DEFAULT);
connection.setRequestProperty("Authorization", encodedAuth);
connection.connect();
int responseCode = connection.getResponseCode();
Can you try by setting the content type:
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
please help me out with this problem.
I want to make a POST call with JSON data that should appear in body to request but i am not able to do that. I made POST call and getting data but data is not in JSON format.
Code :
data = "{'mobile':'"+mobile_number+"','password':'"+mypassword+"'}";
byte[] dataPost = data.getBytes();
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setRequestProperty("Accept", "application/json");
//urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("charset", "UTF8");
urlConnection.setUseCaches(false);
OutputStream os = urlConnection.getOutputStream();
os.write(dataPost);
os.close();
Result is coming like below
body:
{'a':'b','c':'d'}:""
Please Help me. i am new to android Development.
Thanks
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);
...
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