I'm trying to retrieve a json string from a comet URL link.
Here is the API link: http://www.plurk.com/API#realtime
Here is the description:
You'll get an URL from /APP/Realtime/getUserChannel and you do GET requests to this URL to get new data. Your request will sleep for about 50 seconds before returning a response if there is no new data added to your channel. You won't get notifications on responses that the logged in user adds, but you will get notifications for new plurks.
I was able to obtain the comet_server url and paste that to firefox and get the result manually. However, when I tried to get these json string in android, I only got timeout error.
01:48:51.698 com.net.xerothermic.plurk INFO PLURK http://comet58.plurk.com:80/comet?channel=...&offset=0
01:53:43.680 com.net.xerothermic.plurk ERROR PLURK HTTP con. get
response error:Connection timed out
Here is the code I used to retrieve the data.
URL url = new URL(urlString);
HttpURLConnection conn = null;
try
{
conn = (HttpURLConnection) url.openConnection();
}
catch (IOException ex)
{
Log.e("PLURK", "HTTP con. open error:" + ex.getMessage());
return "";
}
try
{
conn.setRequestMethod("GET");
}
catch (ProtocolException ex)
{
Log.e("PLURK", "HTTP con. set method error:" + ex.getMessage());
}
try
{
return conn.getResponseMessage();
}
catch (IOException ex)
{
Log.e("PLURK", "HTTP con. get response error:" + ex.getMessage());
return "";
}
Any suggestion is much appreciated!
EDIT: here is the output from a browser. Did I miss to set some properties?
Even though the timeout value was set to 0 by default (meaning wait infinitely), I found I still need to explicit set the timeout value in order to not raise IOException.
setConnectTimeout(70000);
setReadTimeout(70000);
This is only needed on android but not Windows...
Related
I need help, my app was working fine but after renew of Domain name, I started getting error 301. Is something to be fixed in my below code? Though if I use https://Google.com with same code, response code is 200. Hostgator is also not able to find the issue to fix. Can someone help, pls.
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
URL url = new URL("http://innovativeapps.me");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
int con_respone_code = httpURLConnection.getResponseCode();
Log.i("Response Code: ", con_respone_code + "");
Log.i("Error Message", httpURLConnection.getResponseMessage());
httpURLConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
If i open this using web browser, the url is working fine.
Your site is being redirected to https://innovativeapps.me. When you use the http:// schema, it redirects and returns a 301 letting you know that you need to update your url.
I try to use HttpURLConnection to send a post request to my local (xampp) server with an url like this http://xxx.xxx.0.3/Company/index.php/booking/c200/p-205/2025-02-09 8:2 , the server php file take param in url and send data to mysql database .
The url works fine on postman agent , and even another get method request works smooth in the android application .
Yet when i try the post method with following code :
public void postOrder() {
TextView tv = findViewById(R.id.tv1);
Thread t = new Thread( new Runnable() {
#Override
public void run() {
HttpURLConnection conn = null;
try {
String link = "http://xxx.xxx.0.3/Company/index.php/booking/c200/p-205/2025-02-09 8:2";
URL url = new URL(link);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setReadTimeout(10000 /*ms*/);
conn.setConnectTimeout(15000 /*ms*/);
conn.connect();
}
catch (IOException e) {
Log.d("HTTP error: ", e.toString());
}
finally {
conn.disconnect();
}
}
} );
t.start();
}
It never sent the url and thus no data is stored to database .
And with 6 hours of trial and error , google and searching , i added this line of code :
InputStream is = conn.getInputStream();
And it finally works .
Please answer me why it only works after adding this line of code , what does it do ? I thought the url is triggered right after conn.connect();
Calling connect() only connects, but it doesn't send anything yet. Call getResponseCode() to force the request to be sent. That method is safer than getInputStream() which will throw an exception if the response is not a 2xx (in which case you'd need getErrorStream()).
I'm doing an Android app with an API with Python. The API is on a Google App Engine cloud and everything works fine when I tested it with Postman.
I'm trying to do a Login with a POST method. That method returns json with the user information I keep getting that error: FileNotFoundException
Here is some of my code:
try{
String account = params[0].get(0);
String password = params[0].get(1);
URL url = new URL("http", WEB_SERVICE_URL, PORT, REST_LOGIN);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(CONNECTION_TIMEOUT);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Accept", "application/json");
JSONObject json = jsonParser.serialJsonLogin(nomCompte, motPasse);
osw = new OutputStreamWriter(httpURLConnection.getOutputStream(),"UTF-8");
osw.write(json.toString());
osw.flush();
String body = readStream(httpURLConnection.getInputStream());
osw.close();
Log.i(TAG, "Return : " + body);
user = jsonParser.deserializeJsonUser(body);
}catch (Exception e) {
mException = e;
}finally {
if (mHttpURLConnection != null) {
mHttpURLConnection.disconnect();
}
}
return user;
At: String body = readStream(httpURLConnection.getInputStream()); I'm getting a java.io.FileNotFoundException: http://10.0.2.2:8080/login
My readStream method is fine, I tested it. If I look in my Google App Engine logs, I can see that there is no 404, or anything wrong. If I find the user I get a 201 if not a 403. So even if the error says FileNotFound, I see status code which means that actually the URL is right.
UPDATE: My API was giving me a 201 and getInputStream apparently doesn't work on 201 status. Changed my return status to 200 in my API and it works fine.
Hello all in my doe I have a try catch and I am catching the exception from a webservice
However If I run my web service in Firefox Poster add-on I get a response as well as a stastus exception
This obviously is not ALL the code but basically the exception is happening at getInputStream()
How can I get the response from the exception?
try{
//Get Response
stream = connection.getInputStream();
} catch (Exception e) {
throw new CustomException("Exception - " + e.getMessage());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
String msg = connection.getResponseMessage();
These methods will still throw IOException if you can't reach the server. But if the server responds, even with an error, these methods give you access to the response.
Greetings,
I am trying to setup a server connection from my BlackBerry Application . I was able to get a response code on the status of the server. Now i have a few values which i have to POST to the server
Its like a registration page values(username, password, age ) have to be sent to the server .
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
if (connDesc != null)
{
HttpConnection httpConn;
httpConn = (HttpConnection)connDesc.getConnection();
try
{
final int iResponseCode = httpConn.getResponseCode();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Response code: " + Integer.toString(iResponseCode));
}
});
}
catch (IOException e)
{
System.err.println("Caught IOException: " + e.getMessage());
}
}
Thats the code i used to get the response code.
I would appreciate it if someone could help me how i can make a POST request to the server..
the server url for status was company.com/app/version/stats
when it for register it would be
company.com/app/register
Thank you
What type of a POST do you use? If you are just passing key-value pairs, then it should be a POST of a "application/x-www-form-urlencoded" content-type.
So, what lacks youe code is:
1). Set a proper content-type on your connection:
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
2). Prepare the content to be sent to the server via the POST:
URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("username", username);
encPostData.append("password", password);
encPostData.append("age", age);
byte[] postData = encPostData.toString().getBytes("UTF-8");
3). Set content-length for the connection (this step may be optional - try without this first, probably the BB OS is smart enough to set this automatically):
httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length));
4). Open an OutputStream and write the content to it (the code is simplified):
OutputStream os = httpConn.openOutputStream();
os.write(postData);
os.flush();
...
httpConn = (HttpConnection)connDesc.getConnection();
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("username",name);
httpConn.setRequestProperty("password",pass);
....