Foursquare API 404 error: Endpoint not found - android

I am trying to do a GET request using the foursquare checkin endpoint. I'm getting back a 404 error which is endpoint not found. Any help with why that could be happening would be great!
try {
URI url = new URI("https://api.foursquare.com/v2/user/self/checkins?oauth_token="+TokenStore.get().getToken()+"&v=20140219");
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
is = entity.getContent();
int responceCode = response.getStatusLine().getStatusCode();
Log.i(TAG, "Responce = "+ responceCode);
} catch(Exception e) {
e.printStackTrace();
}

The endpoint is users/self, not user/self, which you have. Just a typo :)

Related

Http request issue

I am unable to get the response from server when I am making request with http in android. I am calling one url that is working on browser, but default http client request giving me reponse code 500 and response is below. What is missing?
<body>
<h1>Cannot read property '_id' of undefined </h1>
<p>[object Object]</p>
</body>
Here is code
HttpClient httpclient = new DefaultHttpClient();
Log.v("", "url is: "+url);
HttpGet httppost = new HttpGet("http://54.255.228.162:3000/item?action=redeem&event=55018105f0df610b1c3919a2");
try {
org.apache.http.HttpResponse response = httpclient.execute(httppost);
int responsecode = response.getStatusLine().getStatusCode();
result = EntityUtils.toString(response.getEntity());
} catch (Exception ex) {
ex.printStackTrace();
}

Socket closed exception when trying to read httpResponse

I have a method to connect to send post data to a webservice and get the response back as follow:
public HttpResponse sendXMLToURL(String url, String xml, String httpClientInstanceName) throws IOException {
HttpResponse response = null;
AndroidHttpClient httpClient = AndroidHttpClient.newInstance(httpClientInstanceName);
HttpPost post = new HttpPost(url);
StringEntity str = new StringEntity(xml);
str.setContentType("text/xml");
post.setEntity(str);
response = httpClient.execute(post);
if (post != null){
post.abort();
}
if (httpClient !=null){
httpClient.close();
}
return response;
}
Then, in my AsyncTask of my fragment, I try to read the response using getEntity():
HttpResponse response = xmlUtil.sendXMLToURL("url", dataXML, "getList");
//Check if the request was sent successfully
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// Parse result to check success
responseText = EntityUtils.toString(response.getEntity());
if (!xmlParser.checkForSuccess(responseText, getActivity())){
//If webservice response is error
///TODO: Error management
return false;
}
}
And when I reach that line:
responseText = EntityUtils.toString(response.getEntity());
I get an exception: java.net.SocketException: Socket closed.
This behavior doesn't happen all the time, maybe every other time.
Just write
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(your url);
HttpResponse response = client.execute(post);
it should work.No need to write codes which makes confusion.
I also experienced the 'socket closed' exception when using a client instance built using HttpClientBuilder. In my case, I was calling HttpRequestBase.releaseConnection() on my request object within a finally block before processing the response object (in a parent method). Flipping things around solved the issue... (working code below)
try {
HttpResponse response = httpClient.execute(request);
String responseBody = EntityUtils.toString(response.getEntity());
// Do something interesting with responseBody
} catch (IOException e) {
// Ah nuts...
} finally {
// release any connection resources used by the method
request.releaseConnection();
}

Status code of HTTP response of server in Android phone is printed in the body of response

I am using http://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/ElementalHttpServer.java for Android.
I set the response using the following code:
HttpResponse getResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 404, "Not Found");
getResponse.setEntity(new StringEntity(new String("The requested resource " + target + " could not be found due to mismatch!!")));
conn.sendResponseHeader(getResponse);
conn.sendResponseEntity(getResponse);
My response in Mozilla Poster or browser has the header 404 and the response body as:
The requested resource could not be found due to mismatch!!HTTP/1.1 200 OK
How can I get only the HTTP body String? Why am I getting HTTP/1.1 200 OK in response. I dont set this in my code. Any help is appreciated.
this might help you...
i think response is what you need
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
}
there was a problem in my defined handle() method. I was creating a new HttpResponse for each request instead of using the response passed in the
public void handle(HttpRequest request, HttpResponse response, HttpContext context)

Android httpclient request and processing with entity

I do requests to my servlet
try {
Log.d(Constants.DEBUG_TAG_MAPS, "try");
HttpEntity entity = new StringEntity(json);
post.setEntity(entity);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
HttpConnectionParams.setSoTimeout(httpParameters, 150000);
httpclient = new DefaultHttpClient(httpParameters);
BasicHttpResponse response = (BasicHttpResponse ) httpclient.execute(post);
Log.d(Constants.DEBUG_TAG_MAPS, "http status code : "+response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200){
Log.d(Constants.DEBUG_TAG_MAPS, "response 200");
HttpEntity responseEntity = response.getEntity();
if (responseEntity!= null) {
Gson g = new Gson();
InputStream is = responseEntity.getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
try{
responseEntity.getContent();
String x = EntityUtils.toString(responseEntity);
}catch (Exception e) {
// TODO: handle exception
}
responseJSONObject = g.fromJson(reader, JSONResponseObject.class);
Log.d(Constants.DEBUG_TAG_MAPS_console, responseJSONObject.toString());
reader.close();
System.gc();
}
}
} catch (Exception e) {
e.printStackTrace();
}
/code>
and on line :
InputStream is = responseEntity.getContent();
app is crashing ...response code is 200 ...
I have looked on my tomcat logs and everything is ok. JSONResponseObject is filled with values and sended.
What could be the problem? How can I get stacktrace to logcat?
One maybe related problem:
I have well know "Couldn't et connection factory client" in logcat
Any of thing could causing that issue is not my case
api key is correct, path to correct debug.keystore
all permissions in manifest
uses-library android:name="com.google.android.maps in manifest as well
Map is displayed correctly, refreshing on zooming/moving.
Problem solved after anotate json objects with google.gson.annotations instead of just implementing serializable.

Error using HTTP Post

I got an error using HttpPost for sending an MMS in Android.
In the Logcat it says:
ERROR/Here(447): ---------Error-----Target host must not be null, or set in parameters.
My sample code:
String url = "myurl";
HttpClient httpClient = new DefaultHttpClient();
try {
httpClient.getParams().setParameter(url, new Integer(90000)); // 90 second
HttpPost post = new HttpPost(url);
File SDCard = Environment.getExternalStorageDirectory();
File file = new File(SDCard, "1.png");
FileEntity entity;
entity = new FileEntity(file,"binary/octet-stream");
entity.setChunked(true);
post.setEntity(entity);
post.addHeader("Header", "UniqueName");
Log.i("MMSHTTP","----post---------------"+post);
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e("Here",
"--------Error--------Response Status line code:" + response.getStatusLine());
}
else
{
// Here every thing is fine.
}
HttpEntity resEntity = response.getEntity();
if (resEntity == null) {
Log.e("Here","---------Error No Response!!-----");
}
} catch (Exception ex) {
Log.e("Here","---------Error-----"+ex.getMessage());
ex.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
How do I fix the error?
The url you're specifying is in your sample code is:
String url = "myurl";
In order for HttpClient to be able to determine the host name, you're going to need to supply a valid url. Something along the lines of:
String url = "http://myurl.com/index";
Note: The 'http://' is important so that the appropriate protocol can be determined.
This guy had the same problem.

Categories

Resources